in reply to Pseudo-hash intrusion...

First, @$foo is an arrayref, not a hashref. You can make it seem like a hashref by the @{$foo} thingies.

Second, just do this.

my @bar = sort{@{$foo->{$a}} cmp @{$foo->{$b}}} @$foo;

I might be wrong on this because I am not too familiar in the area, please point it out to me if I am. Thank you.

UPDATE:jeffa has pointed out to me how I could fix this...
my @bar = sort{$foo->{$a} cmp $foo->{$b}} @$foo;
$_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.

Replies are listed 'Best First'.
Re: Re: Pseudo-hash intrusion...
by Brovnik (Hermit) on Jun 21, 2001 at 19:58 UTC
    This is still broken.
    Inside the sort {}, $a will be one of the list inside $foo, and $b another, so that's how you get the
    No such pseudo-hash field "HASH(0x10a57c)" at ./listref line 22.
    error, because you have coerced the $foo->{$a} to a pseudo-hash reference when it is really an array reference. The error occurs because If $foo were really a pseudo-hash, it should have a hash as the first element, (which it does) which contains a field to match $a which $foo doesn't.

    Within the sort command, $a is already dereferenced, so you must use my @bar = sort {$a->{blah} cmp $b->{blah} } @$foo; Where the names of the fields being compared are the same ('blah' in this case).

    my $foo = [ {blah=>5,d=>1}, {blah=>4,d=>2}, {blah=>3,d=>3}, {blah=>2,d=>4}, {blah=>1,d=>5}]; my @bar = sort {$a->{blah} cmp $b->{blah} } @$foo; print join(',',map {$_->{blah}} @bar), $/;
    worked for me.

    P.S. If this had been an array of arrays, the message is more clear :

    my $foo = [ [5,1], [4,2], [3,3], [2,4], [1,5]]; my @bar = sort {$foo->{$a} cmp $foo->{$a} } @$foo; # should be my @bar = sort {$a[0] cmp $b[0]} @$foo;
    generates Can't coerce array into hash at ./listref line 33. which is a bit more useful.