testing testing... perl question

Andy Armstrong andy at hexten.net
Mon Aug 13 12:18:08 BST 2007


On 13 Aug 2007, at 11:31, Aidan Samuel wrote:

> Hello everyone, I'm new here.
>
> Is it ok to ask a perl question?
>
> perl -e '@a = (1..5); print sort $a <=> $b, @a;'
> 012345

That's sorting a list that contains $a <=> $b (0) spliced to the  
contents of the list.

$ perl -e 'print $a <=> $b'
0

> perl -e '@a = (1..5); print sort {$a <=> $b} @a;'
> 12345

And that's doing the right thing :)

> When using map and grep I tend to leave off the curly braces and just
> use a comma, but if I try this with sort, I get these odd results.

map, grep are special cases in a way that sort isn't. I'd recommend  
passing a block to map, grep too - certainly in all but trivial cases.

-- 
Andy Armstrong, hexten.net



More information about the london.pm mailing list