Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

perlfaq4 error ? good catch: I am stupid

by Zoop (Acolyte)
on Sep 08, 2009 at 07:31 UTC ( [id://794075]=perlquestion: print w/replies, xml ) Need Help??

Zoop has asked for the wisdom of the Perl Monks concerning the following question:

Respected Monks,

I am not sure if its a right place to post this but its something I observed while reading through and I guess it might need correction if at all I am right.

In perlfaq,under the topic

How do I sort an array by (anything)?

Supply a comparison function to sort() (described in sort): 1. @list = sort { $a <=> $b } @list; The default sort function is cmp, string comparison, which would sort (1, 2, 10) into (1, 10, 2) . <=> , used above, is the numerical comparison operator.

Shouldn't it say "would sort (1, 10, 2) into (1, 2, 10)"

All said and done but I must admit about perl that I have not come across any other language (of course out of a handful which I boast I know) that has such an extensive documentation categorized in such a pretty way and accessible in so many ways :)

/zoop

'When You starve With A Tiger, The Tiger always starves last'

Replies are listed 'Best First'.
Re: perlfaq4 error ? good catch: I am stupid
by shmem (Chancellor) on Sep 08, 2009 at 07:35 UTC
    Shouldn't it say "would sort (1, 10, 2) into (1, 2, 10)"

    No. That would be numerical sort - but the default is string comparison, i.e. comparison of chars of strings leftmost first.

      I am stupid :)
      'When You starve With A Tiger, The Tiger always starves last'
        1. you showed initiative
        2. you posted in the right place
        3. your posting was legible and coherent
        4. you didn't assume, you inquired
        5. you learned something new
        No, not stupid at all.

        'When You starve With A Tiger, The Tiger always starves last.' Not if you shoot the tiger first. --Anonymous MJD Fan

        More like: you were ignorent, and in your quest for knowledge you eyes glazed for just a second, your brain played a little trick.

        I have written at least 3 or 4 times as many posts as I have submitted. During the process of writing my post, I have to take a break from 'the problem' in order to sum it up with contextual meaning for the monastery. Often, during this time, I realise my brain's little trick.

        And often it is something just like your post that lies at the core of my problem.

        -=( Graq )=-

Re: perlfaq4 error ? good catch: I am stupid
by AnomalousMonk (Archbishop) on Sep 08, 2009 at 13:48 UTC
    Shouldn't it say "would sort (1, 10, 2) into (1, 2, 10)"
    And, of course, the other approach is: Try it and see:
    >perl -wMstrict -le "my @un_ord = (2, 100, 3, 10, 4, 1); my @n_ord = sort { $a <=> $b } @un_ord; print qq{@n_ord}; my @s_ord = sort { $a cmp $b } @un_ord; print qq{@s_ord}; my @d_ord = sort @un_ord; print qq{@d_ord}; " 1 2 3 4 10 100 1 10 100 2 3 4 1 10 100 2 3 4

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://794075]
Approved by ELISHEVA
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-29 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found