in reply to Error Sorting Array Reference?

Perl is getting confused about which $a you mean - the one that sort uses, or the one that you have declared with my. It is very bad practice to use $a or $b as a variable name, and you have just shown us why!

If you change the code to:

#!/usr/local/bin/perl use strict; use warnings; my $aa = [1, 2, 5, 7, 4]; print sort { $a <=> $b } @$aa;

it works fine with no errors or warnings.

The important thing that you didn't say was what the error was. I get 'Can't use "my $a" in sort comparison', which gives the game away somewhat!


DWIM is Perl's answer to Gödel