sacadura has asked for the wisdom of the Perl Monks concerning the following question:
This node falls below the community's threshold of quality. You may see it by logging in.
Re: array bug?
by hding (Chaplain) on Feb 03, 2003 at 18:39 UTC
|
I'm not sure why you think this is a bug, as you do not say.
It appears to be working correctly to me; perhaps the piece of information that you lack is that sort by default sorts things as strings, which you could discover by reading the documentation for sort. If you want to sort numerically, you can supply your own function or block to do it. | [reply] |
Re: array bug?
by ChemBoy (Priest) on Feb 03, 2003 at 18:43 UTC
|
You don't specify what you think the bug is, but I'm going to assume you're surprised by the sort order. If so, you should reread the documentation on sort: unless you tell it otherwise, it sorts by string comparison, rather than numeric comparison. Since the character value for 3 is less than that of 8, "30" is less than "8" (but greater than "08", of course).
To get the result you're probably looking for, try
foreach (sort {$a <=> $b} @arr) {... }
If God had meant us to fly, he would *never* have given us the railroads. --Michael Flanders | [reply] [d/l] |
Re: array bug?
by broquaint (Abbot) on Feb 03, 2003 at 18:43 UTC
|
This is nothing to do with arrays but how sort processes it's arguments, which is lexically (as opposed to numerically as you were expecting). Here's the relevant paragraph from the sort docs
sort SUBNAME LIST
sort BLOCK LIST
sort LIST
Sorts the LIST and returns the sorted list value.
If SUBNAME or BLOCK is omitted, "sort"s in stan-
dard string comparison order. If SUBNAME is spec-
ified, it gives the name of a subroutine that
returns an integer less than, equal to, or greater
than "0", depending on how the elements of the
list are to be ordered. (The "<=>" and "cmp"
operators are extremely useful in such routines.)
SUBNAME may be a scalar variable name (unsub-
scripted), in which case the value provides the
name of (or a reference to) the actual subroutine
to use. In place of a SUBNAME, you can provide a
BLOCK as an anonymous, in-line sort subroutine.
HTH
_________ broquaint | [reply] [d/l] [select] |
Re: array bug?
by Fletch (Bishop) on Feb 03, 2003 at 18:38 UTC
|
No bug, last time I heard, `4' came before `8'.
Hint: perldoc -f sort
| [reply] [d/l] |
Re: array bug?
by LAI (Hermit) on Feb 03, 2003 at 18:46 UTC
|
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
| [reply] [d/l] [select] |
|
| [reply] |
|
|