Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Get key and value pair from hash without loop

by dirtdog (Monk)
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 (Monsignor) 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 making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2017-10-21 20:02 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (270 votes). Check out past polls.