Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

Get key and value pair from hash without loop

by dirtdog (Scribe)
on Sep 05, 2013 at 16:36 UTC ( #1052600=perlquestion: print w/replies, xml ) Need Help??
dirtdog has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I'm trying to extract the key,value pair from a hash without first starting from a loop

my %VIS; my $key; my $value; my $mfunds="$ENV{'HOME'}/scripts/MFUNDS.txt"; my $evst="$ENV{'HOME'}/scripts/IN_EVST_NOT_IN_NFS.txt"; open MFUND, $mfunds or die "Can not open $mfunds\n"; while ( <MFUND> ) { chomp; my $aref = [split]; $VIS{$aref->[0]} = $aref->[1]; } open EVST, $evst or die "Can not open config file: $evst\n"; while ( <EVST> ) { chomp; my $aref = [split]; if (exists($VIS{$aref->[0]}) ) { print "$key, $value from %VIS\n"; } }

The code sample of course does not work, because i'm not quite sure how to accomplish the task

$mfunds contains 2 columns of data: a number and a description

$evst would contain 1 column of data: a number

so basically, if the number from $evst exists in $mfunds, then print the key, value (number,desc) from $mfunds

any help would be greatly appreciated


Replies are listed 'Best First'.
Re: Get key and value pair from hash without loop
by tangent (Priest) on Sep 05, 2013 at 16:57 UTC
    If $evst contains only one column, no need for the split:
    while ( my $key = <EVST> ) { chomp $key; if (exists($VIS{$key}) ) { print "$key, $VIS{$key}\n"; } }
Re: Get key and value pair from hash without loop
by hdb (Prior) on Sep 05, 2013 at 16:48 UTC

    Hint: $key = $aref->[0];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1052600]
Approved by sparkyichi
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2017-07-24 01:12 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (347 votes). Check out past polls.