http://www.perlmonks.org?node_id=232350


in reply to array bug?

This isn't a bug, it's a feature.

What's happening is that the numbers are getting sorted as strings, and since 8 comes after 30 (chr(8) > chr(3)) (ord(8) > ord(3)), you get those unwanted effects.

Fortunately, (hurrah!) you can pass additional sorting methods to sort. For instance, reverse sort @arr can be rewritten as sort {$b cmp $a} @arr. You can use the same feature to sort numerically using the UFO operator: sort {$a <=> $b} @arr

There's other ways sort simplifies your life; RTFM for more info.

Update: Successfully picked; thanks, ChemBoy... although what I wrote is technically true as well... :o)


LAI
:eof

Replies are listed 'Best First'.
Re^2: array bug?
by ChemBoy (Priest) on Feb 03, 2003 at 18:52 UTC

    Nit: I believe you want ord, not chr. :-)



    If God had meant us to fly, he would *never* have given us the railroads.
        --Michael Flanders