Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: How to compare hash values to array values in Perl

by NetWallah (Abbot)
on May 14, 2014 at 05:28 UTC ( #1085978=note: print w/ replies, xml ) Need Help??


in reply to How to compare hash values to array values in Perl

Since you claim to be a "perl beginner" - kcott's (++) code may be a little esoteric.

Here is somewhat simpler and more traditional code (with no module dependencies):

use strict; use warnings; open(my $table, "<", "table.txt") or die "Cannot open Tables"; my %ids2proteins; while (<$table>){ chomp; my ( $protein, $nbr) = split ; next unless $nbr; $ids2proteins{$nbr} = $protein; } close $table; open(my $val, "<", "values.txt") or die "Cannot open Values"; while (<$val>){ chomp; next unless length $_ > 1; next unless my $prot = $ids2proteins{$_}; print "$prot => $_ \n"; } close $val;

        What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?
              -Larry Wall, 1992


Comment on Re: How to compare hash values to array values in Perl
Download Code
Re^2: How to compare hash values to array values in Perl
by kcott (Abbot) on May 14, 2014 at 06:52 UTC
    "Since you claim to be a "perl beginner" - kcott's (++) code may be a little esoteric."

    To NetWallah: Thanks for the ++; esotericism was not the intention.

    To To_Bz: If there was something in my original code you didn't understand, please feel free to ask; however, first see the clarification below.

    "Here is somewhat simpler and more traditional code (with no module dependencies):"

    The module (Inline::Files) was not intended for To_Bz's code; it was purely for my test.

    I was aiming to use (what appeared to be) the smaller file (values.txt) to create the hash; then selectively printing from (what appeared to be) the larger file (table.txt).

    As a clarification, here's the entire script I envisaged for To_Bz (correcting the open syntax errors and applying my lexical filehandle recommendation):

    #!/usr/bin/env perl -l use strict; use warnings; use autodie; open my $table, '<', 'table.txt'; open my $values, '<', 'values.txt'; my %vals = map { $_ => 1 } <$values>; print $_->[0] for grep { $vals{$_->[1]} } map { [ split / / ] } <$tabl +e>;

    [Note: autodie used in lieu of hand-crafting die messages for both open statements.]

    -- Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1085978]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (17)
As of 2015-07-01 20:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (19 votes), past polls