in reply to Howto Dynamically Create a Hash?
my @nlist = (3,24,8,17,23,14,17,9,16,24,25,11,22,14,14,8,19,16,15,8); my @key_list = ('A'..'Z'); my @lookup; my %hash; NLIST: foreach my $num (@nlist) { foreach (@lookup) { my ($factor, $key) = @$_; if ($num % $factor == 0) { push(@{$hash{$key}}, $num); next NLIST; } } my $key = shift(@key_list); push(@lookup, [$num, $key]); $hash{$key} = [ $num ]; } require Data::Dumper; print(Data::Dumper::Dumper(\%hash));
output: (reformatted for readability)
$VAR1 = { 'A' => [3,24,9,24,15], 'B' => [8,16,8,16,8], 'C' => [17,17], 'D' => [23], 'E' => [14,14,14], 'F' => [25], 'G' => [11,22], 'H' => [19] };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Howto Dynamically Create a Hash?
by ikegami (Patriarch) on Oct 09, 2005 at 21:17 UTC | |
by Aristotle (Chancellor) on Oct 09, 2005 at 23:05 UTC | |
by ikegami (Patriarch) on Oct 10, 2005 at 17:29 UTC |