in reply to Sorting an array of arrays by field

Hue-Bond has already pointed out to the problems with your code, and given a working version.

use warnings; use strict; open my $INF, "<", "look.txt" or die "cannot open"; my @look = <$INF>; close($INF); my @out = sort { (split /\|/, $a)[4] <=> (split /\|/, $b)[5] } @look; for my $line (@out) { my @fields = split /\|/, $line; print join(",", @fields[0 .. 4]), "\n"; } __END__
He's also given a faster solution with both ST and GRT. Thus, there's little left for me to do. However, I can't resist giving a solution using a third, possibly faster sort variant.
use warnings; use strict; open my $INF, "<", "look.txt" or die "cannot open"; my @look = <$INF>; close($INF); my @key = map { (split /\|/)[4] } @look; my @out = @look[sort { $key[$a] <=> $key[$b] } 0 .. @look - 1]; for my $line (@out) { my @fields = split /\|/, $line; print join(",", @fields[0 .. 4]), "\n"; } __END__

I'll also show you a ruby solution as a teaser.

#!ruby -w look = File.open("look.txt").readlines; # here I'm inclined to write ' +map' instead of 'readlines' out = look.sort_by {|line| line.split("|")[4] }; out.each {|line| fields = line.split "|"; puts fields[0 .. 4].join(","); } __END__

That's it. You don't have to listen to the community's whining about strictures, or declare variables, nor do you have to learn complicated idioms for efficently sorting by a field, or quote regexp metacharacters if the language can do that for you. :)