Ordering arrays?

Discussion in 'Perl Misc' started by Gary Mayor, Dec 17, 2003.

  1. Gary Mayor

    Gary Mayor Guest

    Hi,
    I'm trying to get an array into order i've got this array,
    $number[1] = "5";
    $number[2] = "7";
    $number[3] = "4";
    $number[4] = "8";
    $number[5] = "7";

    i'm trying to get it into another array it looks like this,
    $number2[1] = "8";
    $number2[2] = "7";
    $number2[3] = "7";
    $number2[4] = "5";
    $number2[5] = "4";

    So far i've got some code like this,
    for($i=1;$i<=$count;$i++) {
    for($ii=1;$ii<=$count;$ii++) {
    if ($number[$ii]>=$highest[$i]) {
    $number2 = $number[$i];
    print "\n$number2[$i]";
    }
    }
    }

    I've tried the pseudo but could figure out the logic. This is the first
    time i've had to do ordering of this sort so i'm a bit stuck at which
    way to go next. I've checked out cpan but couldn't find anything that
    looked like it would do what i wanted.

    Has anyone got any ideas please.

    Thanks

    Gary
    Gary Mayor, Dec 17, 2003
    #1
    1. Advertising

  2. Gary Mayor <> writes:

    > Hi,
    > I'm trying to get an array into order i've got this array,
    > $number[1] = "5";
    > $number[2] = "7";
    > $number[3] = "4";
    > $number[4] = "8";
    > $number[5] = "7";
    >
    > i'm trying to get it into another array it looks like this,
    > $number2[1] = "8";
    > $number2[2] = "7";
    > $number2[3] = "7";
    > $number2[4] = "5";
    > $number2[5] = "4";


    I think I would do something like this...

    @number2 = reverse sort @number1;

    Ryan
    --
    perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
    Ryan Shondell, Dec 17, 2003
    #2
    1. Advertising

  3. Gary Mayor wrote:
    > I'm trying to get an array into order i've got this array,


    That's called sorting, and there is a function to help you do it:

    http://www.perldoc.com/perl5.8.0/pod/func/sort.html

    This is what I think you are trying to do:

    my @number = (5, 7, 4, 8, 7);
    my @number2 = sort { $b <=> $a } @number;
    print "$_\n" for @number2;

    But you obviously need to study some basics about Perl:

    http://www.perldoc.com/perl5.8.0/pod/perlintro.html

    Good luck!

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 17, 2003
    #3
  4. Gary Mayor

    Gary Mayor Guest

    Now that is wicked five minutes later i had an answer cheers mate that
    works a treat.

    Thanks

    Gary

    Ryan Shondell wrote:
    > Gary Mayor <> writes:
    >
    >
    >>Hi,
    >>I'm trying to get an array into order i've got this array,
    >>$number[1] = "5";
    >>$number[2] = "7";
    >>$number[3] = "4";
    >>$number[4] = "8";
    >>$number[5] = "7";
    >>
    >>i'm trying to get it into another array it looks like this,
    >>$number2[1] = "8";
    >>$number2[2] = "7";
    >>$number2[3] = "7";
    >>$number2[4] = "5";
    >>$number2[5] = "4";

    >
    >
    > I think I would do something like this...
    >
    > @number2 = reverse sort @number1;
    >
    > Ryan
    Gary Mayor, Dec 17, 2003
    #4
  5. Ryan Shondell wrote:
    > Gary Mayor writes:
    >>I'm trying to get an array into order i've got this array,
    >>$number[1] = "5";
    >>$number[2] = "7";
    >>$number[3] = "4";
    >>$number[4] = "8";
    >>$number[5] = "7";
    >>
    >>i'm trying to get it into another array it looks like this,
    >>$number2[1] = "8";
    >>$number2[2] = "7";
    >>$number2[3] = "7";
    >>$number2[4] = "5";
    >>$number2[5] = "4";

    >
    > I think I would do something like this...
    >
    > @number2 = reverse sort @number1;


    A lexical sort of numbers? Not a good advice, is it? (It happens to
    give the same result as a numerical sort applied on the above data,
    since all the values are integers with the same number of digits.)

    perldoc -f sort

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Dec 17, 2003
    #5
  6. Gunnar Hjalmarsson <> writes:

    > A lexical sort of numbers? Not a good advice, is it? (It happens to
    > give the same result as a numerical sort applied on the above data,
    > since all the values are integers with the same number of digits.)


    You're right, of course. I imagine my brain saw the quotes around the
    numbers, and went on "lexical autopilot". :)

    @number2 = sort {$b <=> $a} @number1;

    is a much better solution.

    Ryan
    --
    perl -e '$;=q,BllpZllla_nNanfc]^h_rpF,;@;=split//,
    $;;$^R.=--$=*ord for split//,$~;sub _{for(1..4){$=
    =shift;$=--if$=!=4;while($=){print chr(ord($;[$%])
    +shift);$%++;$=--;}print " ";}}_(split//,$^R);q;;'
    Ryan Shondell, Dec 17, 2003
    #6
  7. Gary Mayor wrote:
    >
    > I'm trying to get an array into order i've got this array,
    > $number[1] = "5";
    > $number[2] = "7";
    > $number[3] = "4";
    > $number[4] = "8";
    > $number[5] = "7";
    >
    > i'm trying to get it into another array it looks like this,
    > $number2[1] = "8";
    > $number2[2] = "7";
    > $number2[3] = "7";
    > $number2[4] = "5";
    > $number2[5] = "4";


    Others have suggested:

    @number2 = sort { $b <=> $a } @number;

    However that populates $number[0] which I see you don't want. Or did
    you think that Perl's arrays started at [1]? Anyway, this will do what
    you want:

    @number2 = ( undef, sort { $b <=> $a } @number );

    Or:

    @number2[ 1 .. $#number ] = sort { $b <=> $a } @number;



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Dec 18, 2003
    #7
  8. Gary Mayor

    Web Surfer Guest

    [This followup was posted to comp.lang.perl.misc]

    In article <brqhgo$gc8$>,
    says...
    > Hi,
    > I'm trying to get an array into order i've got this array,
    > $number[1] = "5";
    > $number[2] = "7";
    > $number[3] = "4";
    > $number[4] = "8";
    > $number[5] = "7";
    >
    > i'm trying to get it into another array it looks like this,
    > $number2[1] = "8";
    > $number2[2] = "7";
    > $number2[3] = "7";
    > $number2[4] = "5";
    > $number2[5] = "4";
    >
    > So far i've got some code like this,
    > for($i=1;$i<=$count;$i++) {
    > for($ii=1;$ii<=$count;$ii++) {
    > if ($number[$ii]>=$highest[$i]) {
    > $number2 = $number[$i];
    > print "\n$number2[$i]";
    > }
    > }
    > }
    >
    > I've tried the pseudo but could figure out the logic. This is the first
    > time i've had to do ordering of this sort so i'm a bit stuck at which
    > way to go next. I've checked out cpan but couldn't find anything that
    > looked like it would do what i wanted.
    >
    > Has anyone got any ideas please.
    >
    > Thanks
    >
    > Gary



    You should use perl's builtin "sort" function. See "perldoc -f sort"
    for further details.
    Web Surfer, Dec 18, 2003
    #8
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,582
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,244
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    561
  4. Philipp
    Replies:
    21
    Views:
    1,123
    Philipp
    Jan 20, 2009
  5. nbigaouette

    Z-Ordering (Morton ordering) question

    nbigaouette, Nov 5, 2009, in forum: C Programming
    Replies:
    2
    Views:
    2,127
Loading...

Share This Page