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; }