Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: sort an array with +ve & -ve numbers in it

by eric256 (Parson)
on Apr 03, 2009 at 15:58 UTC ( [id://755304]=note: print w/replies, xml ) Need Help??


in reply to Re^3: sort an array with +ve & -ve numbers in it
in thread sort an array with +ve & -ve numbers in it

Very true, corrected code:

my @array = ( 99, 67, 0, -100, -38, 98); my ($min, $max) = (sort {$a <=> $b} @array)[0,-1];

Update: Cut n paste error fixed, thanks plobsing and ikegami


___________
Eric Hodges

Replies are listed 'Best First'.
Re^5: sort an array with +ve & -ve numbers in it
by plobsing (Friar) on Apr 05, 2009 at 14:28 UTC

    Typo alert: you have 2 sorts. The result is that you sort alphabetically after having sorted numerically.

    Unfortunately, your test dataset doesn't show the problem. Try adding 100 to the data.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://755304]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-25 23:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found