hotshot has asked for the wisdom of the Perl Monks concerning the following question:
the functions works for hashes like:sub sortHash { my $hash = shift; my @keys = sort { @a = split(/[,-]/, $hash->{$a}); @b = split(/[,-]/, $hash->{$b}); $a[0] cmp $b[0] || $a[1] <=> $b[1] } keys(%{$hash}); return \@keys; }
the function return the keys sorted by the values, for the above hash it will return:my %hash = ( '2.2.2.2' => 'nfs,1', '6.6.6.6' => 'afp-0', '3.3.3.3' => 'nfs-0', '4.4.4.4' => 'cifs,1', '1.1.1.1' => 'cifs,0', );
The problem is that the values in the hash not always contain the index, and I get an use of Uninitialize value in numeric ....my $array = &sortHash(\%hash); print Dumper($array); # will print $VAR = [ '6.6.6.6', '1.1.1.1', '4.4.4.4', '3.3.3.3', '2.2.2.2', ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: sort by hash with existence check
by fruiture (Curate) on Nov 13, 2002 at 11:17 UTC | |
|
Re: sort by hash with existence check
by jdporter (Paladin) on Nov 14, 2002 at 05:33 UTC |