Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Get key and value pair from hash without loop

by dirtdog (Beadle)
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

Tony

Comment on Get key and value pair from hash without loop
Download Code
Re: Get key and value pair from hash without loop
by hdb (Parson) on Sep 05, 2013 at 16:48 UTC

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



Re: Get key and value pair from hash without loop
by tangent (Deacon) 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"; } }

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2014-09-02 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (22 votes), past polls