monkfan has asked for the wisdom of the Perl Monks concerning the following question:
In principle this is how I got those result manually. From @nlist:my $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] };
#!/usr/bin/perl -w use strict; use Data::Dumper; # This is pre-generated random number list, # size could be much greater than this ( >20) my @nlist = (3,24,8,17,23,14,17,9,16,24,25,11,22,14,14,8,19,16,15,8); # This is a pre-generated key candidate. # It may not be used up all of them # In practice I will create a large key list, # that should be greater than potential hash to be created my @key_list = ('A'..'Z'); my $hoa; foreach my $nlist ( @nlist ) { my @tmpar = ($nlist[0]); my $klist; if ( check_member(\@tmpar,$nlist) == 1 ) { push @tmpar, $nlist; $klist = shift @key_list; push @{$hoa->{$klist}}, @tmpar ; } } print Dumper $hoa ; # -- Subroutine ------ sub check_member { # To check if a value can be a member of an array my ($alist,$snum) = @_; if ( $snum % $alist->[0] == 0 ) { return 1; } return 0; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Howto Dynamically Create a Hash?
by Aristotle (Chancellor) on Oct 09, 2005 at 17:08 UTC | |
|
Re: Howto Dynamically Create a Hash?
by ikegami (Patriarch) on Oct 09, 2005 at 17:01 UTC | |
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 | |
|
Re: Howto Dynamically Create a Hash?
by pg (Canon) on Oct 09, 2005 at 17:26 UTC | |
|
Re: Howto Dynamically Create a Hash?
by rir (Vicar) on Oct 10, 2005 at 02:38 UTC |