Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re:

by periapt (Hermit)
on Jun 17, 2004 at 11:59 UTC ( [id://367586]=note: print w/replies, xml ) Need Help??


in reply to de-ref an array from HoA

There are a couple of issues with your code

In your first while loop, you have a bareword sp where you meant $sp. The effect of this would be to assign only one value to each key in the file. Second and subsequent values for a key would be assigned to $p2i{sp}.

In your first foreach loop you try to print the value of $p2i{$key} which is simply a reference to the array you have stored the values in. You have to dereference this value into an array with @{$p2i{$key}}

You have some duplicative code in your if(exists...) loop. You can print the values of the array in $p2i{$user} with the simple foreach loop.

For more information on references, take a look at perldoc -m perlreftut and perldoc -m perlref
while(<FILE>) { chomp; ($sp, $ipr) = split; if (exists $p2i{$sp}) { push @{$p2i{$sp}}, $ipr; # push @{$p2i{sp}}, $ipr; } else { $p2i{$sp}= [$ipr]; } } close FILE; # test to see if hash created correctly foreach $key (sort keys %p2i) { # print "$key\t$p2i{$key}\n"; print "$key\t@{$p2i{$key}}\n"; } if (exists $p2i{$user}) { # print "$user is in the hash. \n";if (exists $p2i{$user}) { print "$user is in the hash. \n"; # @user_array = split(/ /, $p2i{$user}); # @user_array = split(/ /, $p2i{$user}); # foreach (@user_array) { # print "$_\n"; # } foreach (@{$p2i{$user}}){ print "$_ "; } print "\n"; # print "$user: @{ $p2i{$user} }\n"; # only retieves one value even when more than one #val even if the +re are multiple # } } else { print "$user is not in the hash.\n"; } exit;

PJ
We are drowning in information and starving for knowledge - Rutherford D. Rogers
What good is knowledge if you have to pull teeth to get it - anonymous

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-19 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found