I'm trying to sort the printed output of a hash generated by a foreach loop, by the first value of each hash generated.
Here is my original code:
---------------------------------------------
my %output = ();
my @data = ($score,$full_name,$sex,$mobile,$email,$post_code,$uid);
$output{$data[0]} = [ @data ]; # make output hash of @data array
my @row_key = (keys %output);
foreach $row_key (@row_key) {
# get the rest of the data from the output hash
my ($td1, $td2, $td3, $td4, $td5, $td6, $td7) = @{$output{$row_key}};
# make a row of table data to print
my %row = (
td1 => $td1,
td2 => $td2,
td3 => $td3,
td4 => $td4,
td5 => $td5,
td6 => $td6,
td7 => $td7,
);
push(@loop, $td1);
my @sorted = sort { $a->[0] <=> $b->[0] } @loop;
foreach my $i (@sorted) {
print "$i\n";
}
-------------------------------------------
Just trying to sort by the first element of each row (td1) numerically and its not happening. @data is a multidimensional array itself populated by a foreach loop.
Any help hugely appreciated!
Phil Lamond, Manchester, UK.
Here is my original code:
---------------------------------------------
my %output = ();
my @data = ($score,$full_name,$sex,$mobile,$email,$post_code,$uid);
$output{$data[0]} = [ @data ]; # make output hash of @data array
my @row_key = (keys %output);
foreach $row_key (@row_key) {
# get the rest of the data from the output hash
my ($td1, $td2, $td3, $td4, $td5, $td6, $td7) = @{$output{$row_key}};
# make a row of table data to print
my %row = (
td1 => $td1,
td2 => $td2,
td3 => $td3,
td4 => $td4,
td5 => $td5,
td6 => $td6,
td7 => $td7,
);
push(@loop, $td1);
my @sorted = sort { $a->[0] <=> $b->[0] } @loop;
foreach my $i (@sorted) {
print "$i\n";
}
-------------------------------------------
Just trying to sort by the first element of each row (td1) numerically and its not happening. @data is a multidimensional array itself populated by a foreach loop.
Any help hugely appreciated!
Phil Lamond, Manchester, UK.