in reply to Sort multidimensional array by third item

Note that instead of this:

my @line_array = <FILE>; shift(@line_array); pop(@line_array);

You can use an array slice:

@line_array = @line_array[1..-2];  #I guess you can't (see following posts)

Another solution:

... ... my @lines = (<$INFILE>)[1..-2]; my @unsorted = map [split], @lines; my @sorted = sort {$a->[2] <=> $b->[2]} @unsorted; for my $arr_ref (@sorted) { print "@$arr_ref\n"; }

Replies are listed 'Best First'.
Re^2: Sort multidimensional array by third item
by kennethk (Abbot) on Nov 12, 2010 at 22:42 UTC
    Your indices are wrong. The expression 1..-2 returns an empty list. See Range Operators in perlop. This will work if you either use 2 positive (@line_array = @line_array[1 .. @line_array-2];) or 2 negative (@line_array = @line_array[1-@line_array .. -2];) limits on the range.
      Seeing Range operators in perlop is of absolutely no help since it doesn't mention negative indexes.
        From Range Operators in perlop:
        In list context, it returns a list of values counting (up by ones) from the left value to the right value. If the left value is greater than the right value then it returns the empty list.
        Your left value is 1. Your right value is -2. I've made this mistake before.
        A reply falls below the community's threshold of quality. You may see it by logging in.