sort array of hash references

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
 
L

llama

nevernmind...My head just popped out of my ass and all became
clear.

llama wrote:
<snip display-of-ignorance>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top