L
llama
Hello,
I am trying to sort an array of hash references, like what is
returned by "statement_handle->fetchall_arrayref({})" when using
DBI. I cannot do the sort in the SQL call because I am sorting
from the results of two different SQL calls. Here is a code
snippet:
at this point I have two arrays of hash references, namely
"$membrows" and "$survrows". The first thing I do is put the
$membrows data into another hash keyed by "id". This is so
I can do the calculation in the second foreach loop (matching
data from the two datasets via 'id').
#start code fragment....
%membdata;
foreach $r (@$membrows) {
$membdata{$$r{'id'}} = $r;
}
#if sorting by
foreach $sr (@$survrows) {
$rid = $$sr{'id'};
$mr = $membdata{$rid};
$$sr{'notdone'} = $$mr{'tot'} - ($$sr{'finno'} + $$sr{'finyes'});
}
if ($sortby eq 'notdone') {
sort notdonesort @$survrows;
}
#.....at the end of the source file, notdonesort is defined:
sub notdonesort () {
return $$a->{'notdone'} <=> $$b->{'notdone'};
}
#....end code fragment
I've tried various anonymous and declared subroutines passed to
the sort statement and it never sorts. I know the "$$sr{'notdone'}="
is working because I can print out those calculated values later
along with the data that is originally in that hash. I want to
sort the array by those calculated values and I just can't seem to
tell sort about them correctly.
any help would be greatly appreciatee!
thanks.
andrew
I am trying to sort an array of hash references, like what is
returned by "statement_handle->fetchall_arrayref({})" when using
DBI. I cannot do the sort in the SQL call because I am sorting
from the results of two different SQL calls. Here is a code
snippet:
at this point I have two arrays of hash references, namely
"$membrows" and "$survrows". The first thing I do is put the
$membrows data into another hash keyed by "id". This is so
I can do the calculation in the second foreach loop (matching
data from the two datasets via 'id').
#start code fragment....
%membdata;
foreach $r (@$membrows) {
$membdata{$$r{'id'}} = $r;
}
#if sorting by
foreach $sr (@$survrows) {
$rid = $$sr{'id'};
$mr = $membdata{$rid};
$$sr{'notdone'} = $$mr{'tot'} - ($$sr{'finno'} + $$sr{'finyes'});
}
if ($sortby eq 'notdone') {
sort notdonesort @$survrows;
}
#.....at the end of the source file, notdonesort is defined:
sub notdonesort () {
return $$a->{'notdone'} <=> $$b->{'notdone'};
}
#....end code fragment
I've tried various anonymous and declared subroutines passed to
the sort statement and it never sorts. I know the "$$sr{'notdone'}="
is working because I can print out those calculated values later
along with the data that is originally in that hash. I want to
sort the array by those calculated values and I just can't seem to
tell sort about them correctly.
any help would be greatly appreciatee!
thanks.
andrew