#!/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; }