Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Looping with hashes?

by poolpi (Hermit)
on Jan 23, 2009 at 08:28 UTC ( [id://738408]=note: print w/replies, xml ) Need Help??


in reply to Looping with hashes?

If you want to loop over your hashes, you can use references:

for( \%a, \%b, \%c ){ print sort keys %$_, "\n"; }

You can mix all hashes in one
(See above, toolic's node)

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $a = { 1 => 'a', 2 => 'b', 3 => 'c' }; my $b = { 1 => 'd', 2 => 'e', 3 => 'f' }; my $c = { 1 => 'g', 2 => 'h', 3 => 'i' }; my $abc; for my $h_ref ( $a, $b, $c ){ for my $key (keys %$a){ push @{ $abc->{$key} }, $h_ref->{$key}; } } print Dumper $abc; __END__ $VAR1 = { '1' => [ 'a', 'd', 'g' ], '3' => [ 'c', 'f', 'i' ], '2' => [ 'b', 'e', 'h' ] };


hth,
PooLpi

'Ebry haffa hoe hab im tik a bush'. Jamaican proverb

Log In?
Username:
Password:

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

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

    No recent polls found