sirtificate
Programmer
I am trying to develop a Perl script for an genealogical application which I plan to Open Source to store collections of up to 15 hash key / value pairs in approx 500 discrete, indexed, array elements. The hash pairs are collected in the first phase of the script which subsequently iterates over the array and processes the retrieved hash collections, sorting by value and processing the sequenced hash keys during the secod phase.
I've included the code I've put together so far that aims to deal with a single array element, but it fails at the point that I try to put a hash collection into an array element.
I'd really appreciate help with the code to manipulate these data structures - or maybe there is a better way to achieve what I'm trying to do.
#!/usr/bin/perl
use strict;
my ($x, $key, %hash, %new_hash, @array);
sub hashValueAscendingNum
{
$hash{$a} <=> $hash{$b};
}
#Create the hash
for($x=0; $x<5; $x++)
{
$hash{$x} = $x-5;
}
#Print the hash
foreach $key (keys %hash)
{
print "Pre-sort xref - " . $key . " weight - " . $hash{$key} . "\n";
}
#Store the hash collection in an array element
@array[1] = %hash;
print "Array = $array[1]\n";
#Retrieve the hash from the array into a new hash
%new_hash = @array[1];
#Print the new hash in ascending order by hash value
foreach $key (sort hashValueAscendingNum (keys(%new_hash)))
{
print "Post-sort xref - " . $key . " weight - " . $new_hash{$key} . "\n";
}
I've included the code I've put together so far that aims to deal with a single array element, but it fails at the point that I try to put a hash collection into an array element.
I'd really appreciate help with the code to manipulate these data structures - or maybe there is a better way to achieve what I'm trying to do.
#!/usr/bin/perl
use strict;
my ($x, $key, %hash, %new_hash, @array);
sub hashValueAscendingNum
{
$hash{$a} <=> $hash{$b};
}
#Create the hash
for($x=0; $x<5; $x++)
{
$hash{$x} = $x-5;
}
#Print the hash
foreach $key (keys %hash)
{
print "Pre-sort xref - " . $key . " weight - " . $hash{$key} . "\n";
}
#Store the hash collection in an array element
@array[1] = %hash;
print "Array = $array[1]\n";
#Retrieve the hash from the array into a new hash
%new_hash = @array[1];
#Print the new hash in ascending order by hash value
foreach $key (sort hashValueAscendingNum (keys(%new_hash)))
{
print "Post-sort xref - " . $key . " weight - " . $new_hash{$key} . "\n";
}