question about sort()

Discussion in 'Perl' started by Wiseguy, Mar 4, 2005.

  1. Wiseguy

    Wiseguy Guest

    OK. I need to sort a list and I have the following code

    @L=(35,10,0,27,100,-4);
    sub numeric { $A <=> $b; }
    @L=sort numeric @L;

    The only reference I have is an ancient Oreilly book on Perl 4.
    does sort() actually sort the operand list or only return a sorted list?
    Is it safe to use the source operand as the return value of sort() as well?



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
     
    Wiseguy, Mar 4, 2005
    #1
    1. Advertising

  2. Wiseguy

    Jim Gibson Guest

    In article <4227e426$1_2@127.0.0.1>, Wiseguy <>
    wrote:

    > OK. I need to sort a list and I have the following code
    >
    > @L=(35,10,0,27,100,-4);
    > sub numeric { $A <=> $b; }
    > @L=sort numeric @L;
    >
    > The only reference I have is an ancient Oreilly book on Perl 4.
    > does sort() actually sort the operand list or only return a sorted list?
    > Is it safe to use the source operand as the return value of sort() as well?


    One way to find out is try it. If you run the above code, you will get
    some peculiar results. If you want to know why, put 'use strict;' at
    the beginning of your program.

    Another way is to read the documentation that comes with Perl
    installations:

    perldoc -f sort

    although in this case that documentation does not explicitly say that
    it sorts or doesn't sort the original array. So I refer you to method
    1.

    There are newer O'Reilly books on Perl. The curre

    Good luck.

    FYI: this newsgroup is defunct. Try comp.lang.perl.misc in the future
    (but be sure to read the guidelines for that group before posting, as
    you have violated more than one of the strictures for posting to that
    group).


    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
    ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
     
    Jim Gibson, Mar 4, 2005
    #2
    1. Advertising

  3. Wiseguy

    Guest

    it returns a sorted array, it doesn't modify the source

    #!/usr/bin/perl -w
    use strict;
    my @L=(35,10,0,27,100,-4);
    sub numeric { $a <=> $b; }
    @L = sort numeric @L;
    print @L;
     
    , Mar 8, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. nobody
    Replies:
    0
    Views:
    552
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,417
    Dave Moore
    Apr 28, 2004
  3. John Black
    Replies:
    6
    Views:
    2,080
    John Harrison
    May 28, 2004
  4. Angus Comber
    Replies:
    7
    Views:
    1,178
    Richard Heathfield
    Feb 5, 2004
  5. Navin
    Replies:
    1
    Views:
    727
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page