Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
No such thing as a small change
 
PerlMonks  

Re: Pseudo-hash intrusion...

by damian1301 (Curate)
on Jun 21, 2001 at 18:48 UTC ( #90381=note: print w/ replies, xml ) Need Help??


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.


Comment on Re: Pseudo-hash intrusion...
Select or Download Code
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.
    --
    Brovnik

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://90381]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (9)
As of 2014-04-18 07:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (463 votes), past polls