bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians,

I have no problem sorting an AoH, but when I try to sort it as a reference (as I fetchall_arrayref it from MySQL for a HTML::Template loop) I get zero results. I've Googled and SS'ed with little success. Anyway, what am I missing? Thanks.

my $sqldata = [ { level => 2, menuname => 'cccccc' }, { level => 3, menuname => '333333' }, { level => 3, menuname => '222222' }, { level => 1, menuname => 'CCCCCC' }, { level => 2, menuname => 'bbbbbb' }, { level => 1, menuname => 'BBBBBB' }, { level => 1, menuname => 'AAAAAA' }, { level => 3, menuname => '111111' }, { level => 2, menuname => 'aaaaaa' }, ]; my $sorted = sort { $a->{level} <=> $b->{level} || [$a->{menuname} cmp $b->{menuname} } @$sqldata; for ( 0 .. $#$sorted ) { print $sorted->[$_]{level}." ".$sorted->[$_]{menuname}."<br>"; }

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: How to sort a AoH reference
by dragonchild (Archbishop) on Jun 01, 2005 at 14:38 UTC
    my $sorted = []; @$sorted = ...

    Sort returns an array, not an arrayreference.


    • In general, if you think something isn't in Perl, try it out, because it usually is. :-)
    • "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"
Re: How to sort a AoH reference
by Transient (Hermit) on Jun 01, 2005 at 14:41 UTC
    That gives me:
    Useless use of sort in scalar context at ./testsort.pl line 22.
    Changing the "my $sorted" to "my @sorted" (and the like changes below in the foreach loop) gives me sorted results.

    (and I assume the code is not cut-and-pasted, because "[$a->{menuname}" would cause a compile error)
Re: How to sort a AoH reference
by tlm (Prior) on Jun 01, 2005 at 16:34 UTC

    ...or, if you want to retain the rest of your code as you showed it, just enclose the RHS of the assignment to $sorted in []s:

    my $sorted = [ sort { $a->{level} <=> $b->{level} || [$a->{menuname} cmp $b->{menuname} } @$sqldata ];

    the lowliest monk

Re: How to sort a AoH reference
by prasadbabu (Prior) on Jun 01, 2005 at 14:39 UTC

    my guess is, change $sorted into @sorted.

    Prasad

Re: How to sort a AoH reference
by mda2 (Hermit) on Jun 01, 2005 at 17:07 UTC
    I think your question are answered... replace, or initialize scalar variable with an array, to change to array context... (all sort function samples use array context)

    use Data::Dumper; $Data::Dumper::Indent=0; my @x = sort qw( 1 2 3 4 5 ); my $x = sort qw( 1 2 3 4 5 ); my $y = []; @$y = sort qw( 1 2 3 4 5 ); print Dumper(\@x, $x, $y ); __END__ $VAR1 = [ '1', '2', '3', '4', '5' ]; $VAR2 = undef; $VAR3 = [ '1', '2', '3', '4', '5' ];
    But my question... Why don't use mysql todo it as I fetchall_arrayref it from MySQL ?!

    In my concepts... use perl always... when Perl Power is needs!

    --
    Marco Antonio
    Rio-PM

Re: How to sort a AoH reference
by TedPride (Priest) on Jun 01, 2005 at 18:19 UTC
    use strict; use warnings; my $sqldata = [ { level => 2, menuname => 'cccccc' }, { level => 3, menuname => '333333' }, { level => 3, menuname => '222222' }, { level => 1, menuname => 'CCCCCC' }, { level => 2, menuname => 'bbbbbb' }, { level => 1, menuname => 'BBBBBB' }, { level => 1, menuname => 'AAAAAA' }, { level => 3, menuname => '111111' }, { level => 2, menuname => 'aaaaaa' }, ]; print $_->{level}." ".$_->{menuname}."<br>\n" for sort { $a->{level} <=> $b->{level} || $a->{menuname} cmp $b->{menuname} } @$sqldata;