Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: sort mechanism

by satchboost (Scribe)
on Apr 06, 2001 at 20:16 UTC ( [id://70505]=note: print w/replies, xml ) Need Help??


in reply to sort mechanism

This brings up an interesting question. Reading the Library notes on sort seem to indicate that it will return a list (of sorted values).
So, the following code does what you'd think it does: print (3, 2, 1) ? "yes" : "no";
However, if you change it to: print sort (3, 2, 1) ? "yes" : "no"; you get "no", not "yes".

Why does this behavior happen? I'm obviously not understanding something about the sort function.

Replies are listed 'Best First'.
Re: Re: sort mechanism
by MeowChow (Vicar) on Apr 06, 2001 at 21:54 UTC
    I just bumped into this behavior when asking the confused question in Evaluation Disorder. As it turns out, sort always returns false in a scalar context, not the length of the returned list, as one might assume.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
      My question then becomes where would this be documented? How would I known this without playing around? (Granted, the playing was still fun and enlightening, but that's not the point.)
        It should be documented in sort, but it is not. Probably because they didn't think up useful behaviour and didn't want to document intentionally useless behaviour. (Though I would have preferred to see fatally useless behaviour, or at least a warning...

        Personally I think that sort in scalar context should sort the array in place. But that is just me...

Log In?
Username:
Password:

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

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

    No recent polls found