in reply to Re: Print series of numbers
in thread Print series of numbers
sort uses cmp, not <=> (the spaceship operator) by default. Consider:
my @list = (1, 2, 3, 6, 9, 10, 13, 22, 20, 19, 15, 21); printf "<=>: %s\n", join ",", sort { $a <=> $b } @list; printf "cmp: %s\n", join ",", sort { $a cmp $b } @list; printf " : %s\n", join ",", sort @list;
Prints:
<=>: 1,2,3,6,9,10,13,15,19,20,21,22 cmp: 1,10,13,15,19,2,20,21,22,3,6,9 : 1,10,13,15,19,2,20,21,22,3,6,9
printf avoids the need to add a new line to the end of the parameter list after the join. In the OP's code printf seems to me to be more appropriate than print.
Always use strictures (use strict; use warnings; - see The strictures, according to Seuss).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Print series of numbers
by grizzley (Chaplain) on Oct 20, 2009 at 10:52 UTC |