Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

foreach keys over multiple hashes

by c (Hermit)
on Jun 19, 2002 at 20:14 UTC ( #175833=perlquestion: print w/replies, xml ) Need Help??

c has asked for the wisdom of the Perl Monks concerning the following question:

use strict; my %one; my %two; my %three; my @hashes = qw(one two three); for my $i(@hashes) { for my $j(keys %$i) { #do something } }

I get the big gong show 'no strict refs' error on this one. how can i look through a set of hashes using keys, yet keeping them seperate in the loops?

humbly -c

Replies are listed 'Best First'.
(jeffa) Re: foreach keys over multiple hashes
by jeffa (Bishop) on Jun 19, 2002 at 20:18 UTC
    By using references:
    use strict; my %one; my %two; my %three; my $hashes = [\%one, \%two, \%three]; for (@$hashes) { for my $k (keys %$_) { #do something } }
    UPDATE: storing them in an array reference is not necessary, i just wanted to show how to do so. :) But you do have to store the hashes as reference or else they will 'flatten' into the array that contains them.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: foreach keys over multiple hashes
by Aristotle (Chancellor) on Jun 19, 2002 at 20:18 UTC
    All else unchanged: my @hashes = (\%one, \%two, \%three); ____________
    Makeshifts last the longest.
Re: foreach keys over multiple hashes
by thelenm (Vicar) on Jun 19, 2002 at 20:20 UTC
    Instead of using soft symbolic references, you could use hard references. For instance, something like this:
    my %one; my %two; my %three; my @hashes = (\%one, \%two, \%three); for my $hash (@hashes) { for my $key (keys %$hash) { # do something } }

    -- Mike

    --
    just,my${.02}

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2021-06-18 05:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (88 votes). Check out past polls.

    Notices?