Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Sorting an Associative Array?

by target (Beadle)
on Jul 13, 2000 at 02:11 UTC ( [id://22291]=perlquestion: print w/replies, xml ) Need Help??

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

I am a true perl novice and I need to sort the output of this code. I have @array with 96 lines in the array with 15 slices per line (in case I'm using the wrong terminology, $sorted[96][14] is the last referenceable slice. I need to sort by the $array[$j][4] slice before this block executes. I have searched the FAQs, and found several references similar to @sorted = sort {$b <=> $a} @array; but I can not find how to sort by a specific slice.
for $j (0 .. $#sorted) { print "<tr bgcolor=#888888><td>$sorted[$j][4]</td><td>$timecon</td><td +>$sorted[$j][3]</td><td>$sorted[$j][6]</td><td>$sorted[$j][7]. </td><td>$sorted[$j][5].</td><td>$sorted[$j][8]</td><td>$sorted[$j][9] +</td><td>$sorted[$j][11]</td><td>$sorted[$j][12].</td><td>$sor ted[$j][14].</td><td>$sorted[$j][13]</td></tr>\n\n"; }

Replies are listed 'Best First'.
Re: Sorting an Associative Array?
by btrott (Parson) on Jul 13, 2000 at 02:14 UTC
    Try this:
    for my $rec ( sort { $a->[4] <=> $b->[4] } @array ) { ## Now say $rec->[4], $rec->[3], etc. }
    By the way, that's not an associative array. It's a list of lists (see perllol and perldsc).
RE: Sorting an Associative Array?
by awwaiid (Friar) on Jul 13, 2000 at 03:15 UTC

    If its really big go for the Schwartzian Transform. You would end up with something like this:

    @sorted = map {$_->[1]} sort { $a[0] <=> $b->[0] } map { [$_[4],$_] } @unsorted;

    The second line (the "<==> part") might need to be changed if you are sorting something other than numbers. This technique is excelent for very complicated structures, very big structures, and is just neat overall :)

    --awwaiid
      minor correction, you need a '->' in the $a in the sort:
      @sorted = map {$_->[1]} sort { $a->[0] <=> $b->[0] } map { [$_[4],$_] } @unsorted;
      and correct me if I'm mistaken (please), but isn't the Schwartzian Transform used in cases where it is necessary to modify the sort key? ie. dictonary sorting mixed case words.
      /\/\averick

        Thanks for the correction :)

        Naw, the transform doesn't have to modify the sort key... its more general purpose than that (though that is an excellent use). It is just a good algorithm for sorting (array based) data that is more complicated (and or a lot larger) than a plain array. Recently, for instance, I had an array of hashes, and used the transform to sort the array by the contents of one of the hash items.

        For instance, if we had a phone book or something, consisting of an array of hashes, each hash being an entry ($phonebook[0]{firstname} would access the firstname, for example) we could do the transform to sort by say... or something.

        @sorted_phonebook = map {$_->[1]} sort {$a->[0] cmp $b->[0]} map {[$_{lastname},$_]} @phonebook;

        Now, we could indeed do a more complex grabbing of the field we are sorting by and/or a more complex sort algorithm, one in which we needed to modify the key even, but the transform is useful for this case as well.

        --awwaiid
Re: Sorting an Associative Array?
by maverick (Curate) on Jul 13, 2000 at 18:47 UTC
    As far as terminology goes, 'associative array' is another name for 'hash'.
    What you have is an 'array of refrences to arrays'. If you say 'array of arrays' or '2 dimensional array' most everyone will know what you mean :)

    /\/\averick

    not intended to start a terminology holy war

      Awww, can't we -- please?
        Ack -3 and dropping sir!

        Sheesh, can't you all take a kindly joke?

        Ahhh! -4, -5! She can't take much more of it captain!

Re: Sorting an Associative Array?
by target (Beadle) on Jul 13, 2000 at 20:44 UTC
    Thanks for all the help. I learned alot from the answers. I especially liked the links to the correct pages/discussions and the help with terminology. How does that old saying go, "Teach a man to fish...". I still don't understand all that I know about it, but atleast I know a little more to try and understand.
Re: Sorting an Associative Array?
by target (Beadle) on Jul 13, 2000 at 22:57 UTC
    In case anyone has an opinion :) here is how I completed the code. Probably not the best way, but it works for what I want. I installed the following lines before the orignal snippet I sent.
    for $i (0 .. $#origarray) { push @temparray, $origarray[$i][4]; } @sorted = @origarray[ sort { $temparray[$b] cmp $temparray[$a] } 0 .. +$#temparray ];

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://22291]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-26 04:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found