process multiple hashes

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
This may be naive, but here is a situation:

I have a number of hashes stored in a config file, each contains different  
set of data and they all have the same data structure and will be processed
 in the same way. I want to build a loop to process them one at a time. Now
 my question is, is there a way for me to get all there hash names (as hash
 reference I guess), put them in an array so I can build a 'foreach' to pro
cess them? (I have all hash names share something in common, like %AAlevel,
 %BBlevel, %CClevel, etc., and naively wished to grab them by REGEX :)

I did some search and didn't seem to find anything close, and hope to have  
a luck here. Thanks in advance,


Re: process multiple hashes

[Please limit your line length to ~75 characters as has been a tried and
proven custom in Usenet for over 2 decades]

Quoted text here. Click to load it

You are asking for symbolic references. Please see archived articles
about why they are a Very Bad Idea (TM) and what to do instead.


Re: process multiple hashes

Quoted text here. Click to load it

I think it would be good to see a sample of the existing config file,
and any sample code notions to use it.  There are different
possibilities, and by seeing existing code, it might be possible to
suggest something closer to the existing code.

Tim McDaniel,

Re: process multiple hashes

Quoted text here. Click to load it

You need to increase the level of indirection and indexing by one.

In other words, create a %bighash that contains all of the other
hashes, indexed by the name of the hash:

  %bighash = ( AAlevel => \%AAlevel, BBlevel => \%BBlevel, ... );

Then, you can iterate over the keys of big hash, apply your regex to
the names to select them, and process (or not) the individual hashes.

Jim Gibson

Re: process multiple hashes

Quoted text here. Click to load it

You say the file contains hashes, are they perhaps YAML or XML, or  
some other easily parsed data encoding type? If they are then there's
probably a module for that. If they're not, why aren't they?!!!


Justin C, by the sea.

Re: process multiple hashes

Quoted text here. Click to load it

If the hashes are 'globlal' variables, you could loop through all keys
of the symbol table of the package they exist in (this symbol table is
accessible as a hash %package_name::, eg %main:: for package main) and
look for 'well known names', cf (will not compile with strict)

%a_hash = qw(a b c d);
%b_hash = qw(e f g h);
%c_hash = qw(i j k m);

my $cur;
for (keys(%main::)) {
    /_hash$/ or next;
    print("found $_\n");
    $cur = \%$_;
    print("\t$_ => $cur->\n") for keys(%$cur);

NB: This uses a symbolic reference (\%$_) to access the actual
hash. That can be (at least) replaced with


and strict won't complain about that (%main:: can also be accessed as

Alternate idea: Reorganize your config file such that the hashes are
part of some 'top-level data structure', eg (untested)

%hashes = (
    a_hash => {a => 'b', c => 'd'},
        b_hash => {e => 'f', g => 'h'});

This avoids the need to do anything fancy to access the actual hash
given its name and you won't accidentally pick up 'unrelated things with
similar names' which also happen to exist in the symbol table.

Site Timeline