[sf-perl] Sort subroutine didn't return single value at -e line 1.

Uri Guttman uri at stemsystems.com
Fri Apr 5 14:46:47 PDT 2013


On 04/05/2013 05:21 PM, Bill Moseley wrote:
> Could someone explain what's happening here with sort?

>
> $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
> [sort uniq 3,1,3,2]'
> $VAR1 = [
>            1,
>            2,
>            3,
>            3
>          ];
>
>
> Got an extra value there?
>
>
> That code above was in one of our unit tests, and when I ran with 5.16.1 I
> then get:
>
>
> $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
> [sort uniq 3,1,3,2]'
> Sort subroutine didn't return single value at -e line 1.
>
>

sort takes a sub name for its first arg if that is a bareword. so you 
are sorting the whole list but using uniq as the comparison function.

try putting parens around the list so uniq looks like a sub. or parens 
around uniq and the list but that is wrong because sort defaults to 
alpha sorting and that will only work on numbers of the same length.
> This works, though:
>
> $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
> [uniq sort 3,1,3,2]'
> $VAR1 = [
>            1,
>            2,
>            3
>          ];
>
>
well, that is wrong for the same reason as i said above. try it with 10 
also.

uri

>
>
>
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm
>



More information about the SanFrancisco-pm mailing list