process multiple hashes

Joe (archive)

Usenet Archive
Joined
Mar 3, 2000
Messages
612
Reaction score
0
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
 

Tim McDaniel

Usenet Archive
Joined
Sep 12, 2008
Messages
362
Reaction score
0
[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.
 

Jim Gibson

Usenet Archive
Joined
Sep 15, 2003
Messages
1,477
Reaction score
0
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.
 

Justin C

Usenet Archive
Joined
Aug 10, 2005
Messages
416
Reaction score
0
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.
 

Rainer Weikusat

Usenet Archive
Joined
Sep 20, 2006
Messages
1,477
Reaction score
0
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.
 

Rainer Weikusat

Usenet Archive
Joined
Sep 20, 2006
Messages
1,477
Reaction score
0
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 17
Some sort questions - especially hashes 4
hashes 2
hash of hashes 9
FAQ 4.56 How do I merge two hashes? 2
A remark about 'field hashes' 3
ordered hashes 14
hashes 2

Members online

No members online now.

Forum statistics

Threads
548,049
Messages
3,104,245
Members
248,133
Latest member
DujonT

Latest Threads

Top