in reply to sorting comma separated value file

For production use, you might as well use Sort::Fields or Text::CSV as pointed out by merlyn.

But if your purpose is learning, apart from the other solutions mentioned in this thread, you may want to take a look at the following. The line with the assignment to @sorted_lines is called a Schwartzian Transform and is named after its inventor, merlyn himself. For good discussions and descriptions of how this works, see:

And the code is:
#!/usr/local/bin/perl -w use strict; use vars qw($field @lines @sorted_lines); $field=shift @ARGV; @lines=<>; # This is the Schwartzian transform @sorted_lines= map { $_->[0] } sort { $a->[1] cmp $b->[1] } map { [$_, (split(/,\s*/, $_))[$field] ] } @lines; print @sorted_lines;
Once you fully understand the Schwartzian Transform, you'll be on your way to enlightenment :-)

--ZZamboni