process multiple hashes

J

Joe

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 processedin the same way. I want to build a loop to process them one at a time. Nowmy question is, is there a way for me to get all there hash names (as hashreference I guess), put them in an array so I can build a 'foreach' to process 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,

joe
 
T

Tim McDaniel

[Please limit your line length to ~75 characters as has been a tried and
proven custom in Usenet for over 2 decades]
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 process them? (I have all hash
names share something in common, like %AAlevel, %BBlevel, %CClevel,
etc., and naively wished to grab them by REGEX :)

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

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.
 
J

Jim Gibson

Joe said:
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
process 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,

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.
 
J

Justin C

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 process 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,

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.
 
R

Rainer Weikusat

Joe said:
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 process them? (I have all hash names
share something in common, like %AAlevel, %BBlevel, %CClevel, etc.,
and naively wished to grab them by REGEX :)

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

*{$::{$_}}{HASH}

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.
 
R

Rainer Weikusat

Justin C said:
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?!!!

Because perl can parse Perl and Perl supports the necessary syntactical
constructs for describing complex data structures, 'config files' can be
written in Perl and then parsed with perl via do '/etc/something'.

Writing a Perl module in order to parse Perl so that people could
download it instead of using the features built into the perl-program
they already have would surely be the ultimate 'public CPAN masturbation'
exercise. Assuming someone did this (I halfway fear that someone did
really already do this, presumably, as broken XS version supposed to be
fast, as 'pure Perl implementation' which works but is regarded to be
slow and additionally as pure-Perl Perl::Tiny combining the advantages
of both aproaches: It doesn't work and does so slowly), would people
still recommend that?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Iterating hashes 11
Sorting hash of hashes 3
Some sort questions - especially hashes 4
A remark about 'field hashes' 2
ordered hashes 13
hash of hashes 9
Quick sort algorithm 1
Sorting hash of hashes 9

Members online

No members online now.

Forum statistics

Threads
473,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top