First Perl Question - about sorting numeric arrays

Discussion in 'Perl Misc' started by BKennedy, Dec 2, 2003.

  1. BKennedy

    BKennedy Guest

    Hello,

    Well here goes my first newbie PERL question for the gurus of this
    group. My issue is with using the SORT instruction. Below you'll
    find the printed contents of 5 element array before and after a
    'sorting'. Is the 'sorting' being performed on the references and not
    the contents? I don't understand why the numbers are sometimes not in
    proper sequence after the SORT. (Shouldn't the second printing of the
    first array read : 5 9 13 13 14 ?)

    for $a(0..3)
    {
    for($i=0;$i<5;$i++)
    {
    $array[$i] = int(rand(19) +1);
    }
    print("$a\t @array \n");
    @array = sort @array;
    print("$a\t @array \n");
    }

    Results:
    0 14 13 13 9 5
    0 13 13 14 5 9
    1 11 10 5 12 15
    1 10 11 12 15 5
    2 8 16 7 14 13
    2 13 14 16 7 8
    3 12 6 12 5 4
    3 12 12 4 5 6

    Any help is appreciated. Thanks.

    -Bkennedy
     
    BKennedy, Dec 2, 2003
    #1
    1. Advertising

  2. BKennedy

    Jack D. Guest

    "BKennedy" <> wrote in message
    news:...
    > Hello,
    >
    > Well here goes my first newbie PERL question for the gurus of this
    > group. My issue is with using the SORT instruction. Below you'll
    > find the printed contents of 5 element array before and after a
    > 'sorting'. Is the 'sorting' being performed on the references and not
    > the contents? I don't understand why the numbers are sometimes not in
    > proper sequence after the SORT. (Shouldn't the second printing of the
    > first array read : 5 9 13 13 14 ?)
    >
    > for $a(0..3)
    > {
    > for($i=0;$i<5;$i++)
    > {
    > $array[$i] = int(rand(19) +1);
    > }
    > print("$a\t @array \n");


    Stop right here. Read:

    perldoc -q sort

    before you write your next line.

    > @array = sort @array;
    > print("$a\t @array \n");
    > }
    >
    > Results:
    > 0 14 13 13 9 5
    > 0 13 13 14 5 9
    > 1 11 10 5 12 15
    > 1 10 11 12 15 5
    > 2 8 16 7 14 13
    > 2 13 14 16 7 8
    > 3 12 6 12 5 4
    > 3 12 12 4 5 6
    >
    > Any help is appreciated. Thanks.
    >
    > -Bkennedy
     
    Jack D., Dec 2, 2003
    #2
    1. Advertising

  3. BKennedy

    Sam Holden Guest

    On 1 Dec 2003 21:01:32 -0800, BKennedy <> wrote:
    > Hello,
    >
    > Well here goes my first newbie PERL question for the gurus of this
    > group. My issue is with using the SORT instruction. Below you'll
    > find the printed contents of 5 element array before and after a
    > 'sorting'. Is the 'sorting' being performed on the references and not
    > the contents? I don't understand why the numbers are sometimes not in
    > proper sequence after the SORT. (Shouldn't the second printing of the
    > first array read : 5 9 13 13 14 ?)


    No because you are doing a string sort and the string "5" comes
    later than the string "13" when sorted into dictionary order,
    just like "e" comes after "ac".

    perldoc -f sort

    Provides numerous examples on how to use the sort function,
    including sorting numerically.

    You should read the posting guileines that are posted here frequently.
    It is also available at:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Sam Holden
     
    Sam Holden, Dec 2, 2003
    #3
  4. BKennedy

    BKennedy Guest

    (Sam Holden) wrote in message news:<>...
    > On 1 Dec 2003 21:01:32 -0800, BKennedy <> wrote:
    > perldoc -f sort
    >
    > Provides numerous examples on how to use the sort function,
    > including sorting numerically.
    >
    > You should read the posting guileines that are posted here frequently.
    > It is also available at:
    > http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html


    Thank you both and your help got me to my answer. Also, I followed
    the augustmail link and will now abide by the 'rules'. Thanks again.
     
    BKennedy, Dec 3, 2003
    #4
    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. Replies:
    5
    Views:
    955
    X-Centric
    Jun 30, 2005
  2. darrel
    Replies:
    4
    Views:
    841
    darrel
    Jul 19, 2007
  3. jobs

    int to numeric numeric(18,2) ?

    jobs, Jul 21, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    991
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Jul 22, 2007
  4. Philipp
    Replies:
    21
    Views:
    1,157
    Philipp
    Jan 20, 2009
  5. Mike Dershowitz
    Replies:
    2
    Views:
    98
    John Joyce
    Jun 14, 2007
Loading...

Share This Page