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

Hello monks,

This is Pingu, away from his usual machine and he's forgotten his password.

I'm bumbling around with Text::xSV, currently writing a new package to provide webpage based searching of xSV files. I've got my output from xSV as a hash of arrays, and I want to sort it by one of the array values. I have:

... my $csv = Text::xSV->new(); my %results; my $count; $csv->bind_header(); while ($csv->get_row()) { @row =$csv->extract(@headers); $results{$count++} = [ @row ] unless ($notfound); } $self->{results} = \%results; ... my $results = $self{results}; foreach (sort { return $$results{$a}[0] <=> $$results{$b}[0]; } ke +ys %$results) { for (my $i=0; $i<@$headers; $i++) { print $$results{$_}[$i]."\n"; } }
Which blatantly refuses to work. I've used a similar technique before, but that was with a straight hash, not a reference to a hash? Does it make a difference??

Please help pingu so he can go to sleep happy tonight.

---

All things in moderation, including moderation.

Replies are listed 'Best First'.
Re: Pingu can't sort a hash
by jarich (Curate) on Nov 25, 2001 at 08:54 UTC
    Hi Pingu, here follows one of the very many ways to do it.
    #!/usr/bin/perl -w use strict; # Saves me from worrying about csv stuff. # $results is a reference to a hash. my $results = { x => [1,3,4,5,6], y => [14,6,7,8,5], z => [6,7,8,5,3] }; # first we dereference our hash, then we check # our index... foreach (sort { return $$results{$a}->[0] <=> $$results{$b}->[0]; } keys %$results) { print "key = $_\narray =\n"; foreach my $i (0..4) { print $$results{$_}->[$i]."\n"; } }
    Works like a charm for me. :)
Re: Pingu can't sort a hash
by dvergin (Monsignor) on Nov 25, 2001 at 09:19 UTC
    Here's an approach:
    use strict; my $hoa_ref = {Charlie => [1,2,3,4], Andrew => [9,8,7,6], Daniel => [5,6,2,3]}; for my $key (sort {$hoa_ref->{$a}[0] <=> $hoa_ref->{$b}[0]} keys %$hoa +_ref) { # prove it worded... print "$key:"; for my $item ( @{$hoa_ref->{$key}} ) { print " $item"; } print "\n"; }
    Update: Dratted jarich, beat me while I was working it out. ;-)
Re: Pingu can't sort a hash
by seanbo (Chaplain) on Nov 27, 2001 at 21:04 UTC
    Here is something I did once while sorting a hash keyed by IP address. Just to show another way of sorting things out. The results above are both very good replies and are detiled to your specific question, but for what it's worth...here is my code.
    sub sort_ips() { my %counts; my $hash_ref = shift; print "Subnet\t\tCount\n"; foreach my $ip(keys(%$hash_ref)) { my $subnet = pack 'C3', split /\./, $ip; $counts{$subnet}++; } foreach my $subnet(sort keys(%counts)) { print join '.', unpack 'C3', $subnet; print ":\t$counts{$subnet}"; print "\n"; } print "\n"; }


    seanbo
Re: Pingu can't sort a hash
by Pingu (Sexton) on Nov 30, 2001 at 19:34 UTC
    seanbo, jarich, dvergin - thanks for all your help.

    I think my main problem was trying to sort text using the spaceship operator - very silly indeed and guaranteed to screw up... I've fixed the sort routine so that it sorts asciibetically and then numerically which seems to have solved everything.

    ---

    Pingu