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

Uri Guttman uri at stemsystems.com
Fri Apr 5 17:13:13 PDT 2013


On 04/05/2013 06:13 PM, Bill Moseley wrote:
> On Fri, Apr 5, 2013 at 2:46 PM, Uri Guttman <uri at stemsystems.com> wrote:
>
>>
>> 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.
>
>
> Oh, I had tried parens, but I guess it's still seen as a barword.
>
> $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
> [sort uniq( 3,1,3,2)]'
> $VAR1 = [
>            1,
>            2,
>            3,
>            3
>          ];
>
> as well as "sort (uniq 3,1,3,2)"

seems to be parsed that way.
>
>
>
> Does the plus sign tell sort it's not a bareword in this case?
>
> $ perl -wle 'use Data::Dumper; use List::MoreUtils "uniq";  print Dumper
> [sort +uniq 3,1,3,2]'
> $VAR1 = [
>            1,
>            2,
>            3
>          ];
>
yes. you can always make any bareword into a function call with a 
preceding plus. it is a noop from a semantic view but it lets the parser 
know it is an expression and not a bareword.

> Yes, still need to deal with the numeric sort, but I was just using that
> list as an example.

if you had the numeric comparison block as you know about, this problem 
would never have surfaced. using good example data is important too.

uri



More information about the SanFrancisco-pm mailing list