Loading ...
Sorry, an error occurred while loading the content.

Multiple Dimension Array with multiple sort

Expand Messages
  • Tim Lewis
    This may be a simple question, but I have searched for this answer. Suppose I have a multi-di array. I can make it sort by any one element. But what I need
    Message 1 of 12 , Apr 7, 2011
    • 0 Attachment
      This may be a simple question, but I have searched for this answer.
      Suppose I have a multi-di array. I can make it sort by any one element. But what I need to sort by two? In my simple example below, what if I wanted to sort by the animal name, and then by number of legs? I am grateful for any help or tips on this.

      #!/usr/local/bin/perl
      use strict;
      use warnings;

      my @critters; #multi-di with animal name and number of legs
      $critters[0][0] = "dog";
      $critters[0][1] = "4";
      $critters[1][0] = "cat";
      $critters[1][1] = "4";
      $critters[2][0] = "monkey";
      $critters[2][1] = "2";

      #I know I can sort by any one field with
      @critters = sort { $a->[0] cmp $b->[0] } @critters;
      my $critterCount= @critters;
      for (my $count=0;$count<$critterCount;$count++) {
      print "The critter $critters[$count][0] has $critters[$count][1] legs\n";
      }
    • merlyn@stonehenge.com
      ... Tim Suppose I have a multi-di array. I can make it sort by any one Tim element. But what I need to sort by two? In my simple example Tim below, what
      Message 2 of 12 , Apr 7, 2011
      • 0 Attachment
        >>>>> "Tim" == Tim Lewis <twlewis@...> writes:

        Tim> Suppose I have a multi-di array. I can make it sort by any one
        Tim> element. But what I need to sort by two? In my simple example
        Tim> below, what if I wanted to sort by the animal name, and then by
        Tim> number of legs? I am grateful for any help or tips on this.

        Since that can be interpreted two ways, I'm going to pretend you
        actually said "with the major sort by animal name, using the number of
        legs to break ties as a secondary sort".

        Tim> my @critters; #multi-di with animal name and number of legs
        Tim> $critters[0][0] = "dog";
        Tim> $critters[0][1] = "4";
        Tim> $critters[1][0] = "cat";
        Tim> $critters[1][1] = "4";
        Tim> $critters[2][0] = "monkey";
        Tim> $critters[2][1] = "2";

        my @sorted = sort {
        $a->[0] cmp $b->[0]
        or $a->[1] <=> $b->[1];
        } @critters;

        This is covered in the Alpaca book.

        And now, for the traditional "don't post here" comment:

        **************************************************
        *
        * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
        *
        * The official one is at beginners@..., with instructions on how to
        * join at http://lists.perl.org/list/beginners.html.
        *
        * You'll get far better response there, not to say you won't get *any* here.
        *
        **************************************************


        --
        Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
        <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
        Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
        See http://methodsandmessages.posterous.com/ for Smalltalk discussion
      • Tim Lewis
        Thanks Randall. I was close. I was using cmp instead of for the number portion. Thanks again. I did not know about the other listserver, and found this
        Message 3 of 12 , Apr 7, 2011
        • 0 Attachment
          Thanks Randall. I was close. I was using "cmp" instead of <=> for the number portion.

          Thanks again.
          I did not know about the other listserver, and found this one on Yahoo. I will check out the other one.


          Tim

          ---- merlyn@... wrote:
          > >>>>> "Tim" == Tim Lewis <twlewis@...> writes:
          >
          > Tim> Suppose I have a multi-di array. I can make it sort by any one
          > Tim> element. But what I need to sort by two? In my simple example
          > Tim> below, what if I wanted to sort by the animal name, and then by
          > Tim> number of legs? I am grateful for any help or tips on this.
          >
          > Since that can be interpreted two ways, I'm going to pretend you
          > actually said "with the major sort by animal name, using the number of
          > legs to break ties as a secondary sort".
          >
          > Tim> my @critters; #multi-di with animal name and number of legs
          > Tim> $critters[0][0] = "dog";
          > Tim> $critters[0][1] = "4";
          > Tim> $critters[1][0] = "cat";
          > Tim> $critters[1][1] = "4";
          > Tim> $critters[2][0] = "monkey";
          > Tim> $critters[2][1] = "2";
          >
          > my @sorted = sort {
          > $a->[0] cmp $b->[0]
          > or $a->[1] <=> $b->[1];
          > } @critters;
          >
          > This is covered in the Alpaca book.
          >
          > And now, for the traditional "don't post here" comment:
          >
          > **************************************************
          > *
          > * THIS IS NOT THE OFFICIAL perl BEGINNERS GROUP.
          > *
          > * The official one is at beginners@..., with instructions on how to
          > * join at http://lists.perl.org/list/beginners.html.
          > *
          > * You'll get far better response there, not to say you won't get *any* here.
          > *
          > **************************************************
          >
          >
          > --
          > Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
          > <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
          > Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
          > See http://methodsandmessages.posterous.com/ for Smalltalk discussion
        Your message has been successfully submitted and would be delivered to recipients shortly.