in reply to How can I sort this data on the first field

I know there are several working examples above, but they all seem to be longer, more obfuscated, or don't include the creation code for the array. Props to the guy with the table code, though :) Here's mine.
use strict; use warnings; my ($c, @data); while (<DATA>) { chomp; $data[$c++] = [split /\|/]; } print join('|', @$_) . "\n" for (sort {@$a[0] <=> @$b[0]} @data); __DATA__ 30|microsoft 70|aol 76|netscape 35|mozilla 40|opera

Replies are listed 'Best First'.
Re^2: How can I sort this data on the first field
by Chady (Priest) on Apr 25, 2005 at 09:48 UTC

    The funny thing is that your version can be extremely shortened to this:

    print sort <DATA>; __DATA__ 30|microsoft 70|aol 76|netscape 35|mozilla 40|opera

    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
    Chady | http://chady.net/
    Are you a Linux user in Lebanon? join the Lebanese Linux User Group.

      The example can, but as soon as you have numbers that are a different order of magnitude, it will fail:

      print sort <DATA>; __DATA__ 9|microsoft 70|aol 108|netscape 35|mozilla 40|opera

      Based on the similarity of this example, with that in Need help with searching flatfile and updating it., I can only assume that the numbers aren't going to remain static.

        I left this out on purpose :)

        You still can do it without manipulating the original data.

        print sort { $a <=> $b } <DATA>; __DATA__ 9|microsoft 70|aol 108|netscape 35|mozilla 40|opera

        The main issue here is what you want to do with the data at the end, since you're printing it in the same format as it started with, then you don't need to split it only to join it back at the end since sort will work either way.


        He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.
        Chady | http://chady.net/
        Are you a Linux user in Lebanon? join the Lebanese GNU/Linux User Group.