Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

problem using foreach and each with hash ref

by vinoth.ree (Monsignor)
on Aug 19, 2009 at 05:41 UTC ( [id://789690]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

use strict; use warnings; use Data::Dumper; my $hash_ref={1002=>[1,2,3,4,5],1001=>[6,7,8,9,10],3=>[2]}; my ($key,$val); foreach(($key,$val)=each %$hash_ref){ print "Each()=======================>$key=>$val<================== +=====\n"; print "Default>>>>>>>>>>>>>>>>$_<<<<<<<<<<<<<<<<<<\n"; }

I just tried to get the pair of key and value from the hash reference, I got what I want using the while loop, when I go for 'foreach' I count not understand what is happening ? Any idea pl!

Update

Title changed.

Replies are listed 'Best First'.
Re: What is happening ?
by tilly (Archbishop) on Aug 19, 2009 at 05:48 UTC
    You are assigning a single key/value pair to $key and $value, then making a list of 2 elements out of that list, then iterating over that list of 2 elements.

    The next time you call each you'll get the next pair out of the hash.

    The usual way to do what you likely want is:

    foreach my $key (keys %$hash_ref) { my $value = $hash_ref->{$key}; ... }
      The next time you call each you'll get the next pair out of the hash.

      I get printed only, what the each returns and foreach get ends.

      Update:
      Title updated
        You most likely want to use something like this:
        while (($key, $value) = each %$hash_ref) { # do something... }
        An example like this is also mentioned in the documentation for each. tilly already explained the reason your loop ends after the first key and value.
        Yes, executing that loop only called each once. But if after that loop you put a second loop that calls each, then that second loop will get a second pair out of the hash. Or if you try to iterate through with a while/each loop, you'll get every pair except the one already consumed in the for loop.

        In other words the hash has been left in a potentially bad state.

Re: What is happening ?
by Marshall (Canon) on Aug 19, 2009 at 09:37 UTC
    Not sure what you want:
    There are lots of ways to sort this stuff once we agree on the data to be output. Since $rHol is a reference, you need the -> operator to deference the key, normal hash would be just $Hol{$key}, but you need $rHoL->{$key}. Then you need {} around that whole thing to tell the @ operator what to operate upon.

    #!/usr/bin/perl -w use strict; my $hash_ref= {1002=>[1,2,3,4,5], 1001=>[6,7,8,9,10], 3 =>[2]}; print_HoL($hash_ref); sub print_HoL { my $rHoL = shift; foreach my $key (keys %$rHoL) { print "$key: @{$rHoL->{$key}}\n"; } } __END__ Prints: 1001: 6 7 8 9 10 1002: 1 2 3 4 5 3: 2
Re: problem using foreach and each with hash ref
by sanku (Beadle) on Aug 21, 2009 at 12:34 UTC
    hi, I think it's not that much easy to both key and value pair for hash but you can get either key or value. In my code i used the keys indexes to get the values of hash reference.
    use strict; use warnings; use Data::Dumper; my $hash_ref={1002=>[1,2,3,4,5],1001=>[6,7,8,9,10],3=>[2]}; my ($hashref,$array_ref,@array,); foreach $hashref( keys %$hash_ref){ $array_ref=$$hash_ref{$hashref}; @array=@$array_ref; print "Key = ".$hashref."\tValue = "; foreach (@array){ print "$_ \t"; } print "\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found