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

Hi, I am trying to sort the zeroth array element having a column of numbers(both repititive and unique) to get the top 20 maximum values.I used sort function to sort the numbers in numerical descending but does not work on the array element. Any help would be appreciated.

@sort = sort {$b <=> $a} @array;
Thanks

Replies are listed 'Best First'.
Re: sorting the column of array element
by sauoq (Abbot) on May 24, 2012 at 00:11 UTC
    Hi, I am trying to sort the zeroth array element

    By this do you mean that your $array[0] is a reference to an array?

    If so, you want something like

    @sort = sort {$b <=> $a} @{ $array[0] };

    -sauoq
    "My two cents aren't worth a dime.";

      Hey Thanks.. Yes $array[0] is one column of a tab delimited file having three columns.so i wanna sort on the first column Thanks

        Ummmmm..... it seems to me that either your answer, "yes," to the question whether @array[0] 'is a reference to an array?'
        • is inaccurate (eg, 'wrong')
               . ... or else
        • @array[0] is something other than the simple content of 'one column of a tab delimited file having three columns' (where the 'simple content' is something sortable -- either numericly or alphabetically -- without further specification of the criteria for sorting).

        Which is it?

        Update: Added quoted content in second bullet, for clarity and closing paren for correctness. Duh!

Re: sorting the column of array element
by Anonymous Monk on May 24, 2012 at 11:31 UTC

    You're being unclear in what kind of data structure you are holding (try dumping it with Data::Dumper). However, I am guessing you have an array of arrayrefs.

    The magic variables $a and $b in sort are aliases to the array's elements. Since the element is an arrayref, you need to dereference it. If you were to access the first column normally in your code, you'd say $array[$x]->[0]. Through the aliasing, $array[$x] is equal to $a or $b. Therefore, the correct syntax should be $a->[0]. So,

    sort { $b->[0] <=> $a->[0] } @array

    might do the trick.

      However, I am guessing you have an array of arrayrefs.

      ...

      sort { $b->[0] <=> $a->[0] } @array

      Assuming, like you said, an array of arrayrefs—which is a good assumption, I think—this code would try to order the array refs in @array by the first elements contained in the arrays they reference.

      That seems very unlikely to be what he wants.

      Update:

      If he wants to sort the array referenced by $array[0], the code I gave will work. If he wants to sort all of his "columns," he should sort the indexes based on his sorting column using something like:

      my @indexes = sort {$array[0]->[$b] <=> $array[0]->[$a]} (0..$#{$array +[0]});
      And then use the array of sorted indexes to map the sorted order onto the actual columns with something like this:
      my @sort = map { my $a = $_; [ map { $t->[$_] } @indexes ] } @array;

      It's up to him to be sure the "columns" are the same length, of course.

      -sauoq
      "My two cents aren't worth a dime.";
        To clear, i have to sort the $array[0] which is a column of numbers out of three columns of a file.None of the code above works actually...dunno. Thanks
Re: sorting the column of array element
by locked_user sundialsvc4 (Abbot) on May 24, 2012 at 14:15 UTC

    Notice how the second parameter of sort is enclosed in curly-brackets.   That’s because it is actually an executable subroutine or block, which is obliged to return a value that is less, greater, or equal to zero.   Although operators such as <=> are helpfully provided for your convenience, the comparison function can actually be anything that it needs to be.   This is how you can, for example, do multi-level sorts (more than one key), or sort on particular elements of an array, object, or hash.

    Particularly if you perform the same sort the same way in more than one place in your program, seriously consider defining a single sub that you can write just one time and then reference many times throughout your program.

Re: sorting the column of array element
by MajingaZ (Beadle) on May 24, 2012 at 15:21 UTC
    As you can see alot of monks are guessing about what you are storing. To get more accurate results,
    1. Show more code
    2. Provide examples of the data being stored and what your desired output is