in reply to Storing an indeterminate number of hash keys in a variable
This feels like the wrong solution to the problem as the results are very messy. This seems like a good situation for a struct type solution:
use strict; use warnings; # make a little mini class for your records use Class::Struct Record => [title => '$', rule_severity => '$', rule_ +weight => '$' ]; my %data_hash = (); my @data_array = (); $data_hash{first_key} = new Record; $data_hash{second_key} = new Record; # instead of pushing a text string of keys, just push field to access push(@data_array, 'title'); push(@data_array, 'rule_weight'); # some code later changes the hash $data_hash{first_key}->title('Mr. President'); $data_hash{first_key}->rule_weight(100); $data_hash{second_key}->title('Emperor'); $data_hash{second_key}->rule_weight(1000); # want to access it from your array for my $key (sort keys %data_hash) { for my $field (@data_array) { print "My data is (".$data_hash{$key}->$field.")\n"; } }
Output:
C:\tmp>tmp.pl My data is (Mr. President) My data is (100) My data is (Emperor) My data is (1000)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Storing an indeterminate number of hash keys in a variable
by tj_thompson (Monk) on Jul 08, 2011 at 22:42 UTC | |
by muba (Priest) on Jul 09, 2011 at 02:25 UTC | |
by planetscape (Chancellor) on Jul 09, 2011 at 13:43 UTC | |
by tj_thompson (Monk) on Jul 12, 2011 at 16:24 UTC |