sorting array of hash in hash

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dillipkumar
    New Member
    • Mar 2008
    • 41

    sorting array of hash in hash

    Hi,
    I have a Hash:

    Code:
    %hash= 'student'  => [
                                        {
                                           'roll_no' => 10,
                                           'sub' => 'eng'
                                           marks => 32,
                                        },
                                        {
                                           'roll_no' => 11,
                                           'sub' => 'math'
                                           marks => 69,
                                        },
                                        {
                                           'roll_no' => 10
                                           'sub' => 'science'
                                           marks => 69,
                                        },
    
                                        {
                                           'roll_no' => 25
                                           'sub' => 'geo'
                                           marks => 59
                                        },
    
                                    ],

    I want to sort the hash according to sorting of 'roll_no' ..
    After sorting It should be displayed

    Code:
    %hash= 'student'  => [
                                        {
                                           'roll_no' => 10,
                                           'sub' => 'eng'
                                           marks => 32,
                                        },
                                         {
                                           'roll_no' => 10
                                           'sub' => 'science'
                                           marks => 69,
                                        },
    
                                         {
                                           'roll_no' => 11,
                                           'sub' => 'math'
                                           marks => 69,
                                        },
                                       
                                        {
                                           'roll_no' => 25
                                           'sub' => 'geo'
                                           marks => 59
                                        },
    
                                    ],
    Can anybody help me...
    Last edited by eWish; Apr 1 '08, 12:09 AM. Reason: Please use code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Is this school/class/course work?

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      A hash has no order. If you want to have data sorted, use an array instead.

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        Originally posted by Ganon11
        A hash has no order. If you want to have data sorted, use an array instead.
        It is possible but I would also recommend using an array. See Tie::SortHash and Tie::IxHash.

        Comment

        • dillipkumar
          New Member
          • Mar 2008
          • 41

          #5
          Hi,
          Actually this is a task..
          The Original Hash is like this:
          [CODE=perl]%hash = ( 'difference' => [
          {
          'line_number' => '21',
          'filename' => 'BRANCH27',
          'source_line' => {},
          'target_line' => '001S TR'
          },
          {
          'line_number' => '1',
          'filename' => 'BRANCH',
          'source_line' => {},
          'target_line' => '023X '
          },
          {
          'line_number' => '140',
          'filename' => 'BRANCH',
          'source_line' => {},
          'target_line' => '139FIFTH'
          },
          {
          'line_number' => '1',
          'filename' => 'ACCOUNT27',
          'source_line' => {},
          'target_line' => '0223X '
          },
          ],

          );[/CODE]
          i want to sort this hash..
          Last edited by eWish; Apr 2 '08, 01:20 AM. Reason: Please use code tags

          Comment

          • dillipkumar
            New Member
            • Mar 2008
            • 41

            #6
            Originally posted by Ganon11
            A hash has no order. If you want to have data sorted, use an array instead.

            Hi The Original hash is like this:
            Code:
            %hash = ( 'difference' => [
            								{
            								  'line_number' => '21',
            								  'filename' => 'BRANCH27',
            								  'source_line' => {},
            								  'target_line' => '001S TR'
            								},
            								{
            								  'line_number' => '1',
            								  'filename' => 'BRANCH',
            								  'source_line' => {},
            								  'target_line' => '023X '
            								},
            								{
            								  'line_number' => '140',
            								  'filename' => 'BRANCH',
            								  'source_line' => {},
            								  'target_line' => '139FIFTH' 
            								},
            								{
            								  'line_number' => '1',
            								  'filename' => 'ACCOUNT27',
            								  'source_line' => {},
            								  'target_line' => '0223X '
            								},
            							 ], 
            	
            			 );
            Last edited by eWish; Apr 2 '08, 01:20 AM. Reason: Please use code tags

            Comment

            • nithinpes
              Recognized Expert Contributor
              • Dec 2007
              • 410

              #7
              Originally posted by dillipkumar
              Hi The Original hash is like this:
              %hash = ( 'difference' => [
              {
              'line_number' => '21',
              'filename' => 'BRANCH27',
              'source_line' => {},
              'target_line' => '001S TR'
              },
              {
              'line_number' => '1',
              'filename' => 'BRANCH',
              'source_line' => {},
              'target_line' => '023X '
              },
              {
              'line_number' => '140',
              'filename' => 'BRANCH',
              'source_line' => {},
              'target_line' => '139FIFTH'
              },
              {
              'line_number' => '1',
              'filename' => 'ACCOUNT27',
              'source_line' => {},
              'target_line' => '0223X '
              },
              ],

              );
              The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference '), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
              You can use:
              Code:
              foreach $k (keys %hash) {
              print "\n\n$k \n\n";
               foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
                print "$_   : $a->{$_}\n" foreach(keys %{$a});
                print "\n";
                 }
              }

              Comment

              • dillipkumar
                New Member
                • Mar 2008
                • 41

                #8
                Originally posted by nithinpes
                The structure is hash of array of hashes. To display it sorted according to 'line_number' foreach top-level key('difference '), you would be actually sorting the array and not the hash (according to the intended output you posted in your initial description).
                You can use:
                Code:
                foreach $k (keys %hash) {
                print "\n\n$k \n\n";
                 foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
                  print "$_   : $a->{$_}\n" foreach(keys %{$a});
                  print "\n";
                   }
                }
                Hi Nitin,

                Now it is working but the 'source_line' returns the hash like:
                source_line : HASH(0x81ffdac)

                Comment

                • nithinpes
                  Recognized Expert Contributor
                  • Dec 2007
                  • 410

                  #9
                  Originally posted by dillipkumar
                  Hi Nitin,

                  Now it is working but the 'source_line' returns the hash like:
                  source_line : HASH(0x81ffdac)
                  That is because you have an empty hash as value for 'source_line' key.

                  Comment

                  • dillipkumar
                    New Member
                    • Mar 2008
                    • 41

                    #10
                    Originally posted by nithinpes
                    That is because you have an empty hash as value for 'source_line' key.
                    Actually after executing it is displaying this format -->
                    Code:
                    {
                    filename' => 'BRANCH27',
                    line_number' => '21',
                    'source_line' => {},
                    'target_line' => '001S TR'
                    }
                    Code:
                    But the Actually Hash is in this format:
                    {
                    'line_number' => '21',
                    'filename' => 'BRANCH27',
                    'source_line' => {},
                    'target_line' => '001S TR'
                    },
                    Last edited by eWish; Apr 2 '08, 01:21 AM. Reason: Please use code tags

                    Comment

                    • nithinpes
                      Recognized Expert Contributor
                      • Dec 2007
                      • 410

                      #11
                      Originally posted by dillipkumar
                      Actually after executing it is displaying this format -->

                      {
                      'line_number' => '21',
                      'filename' => 'BRANCH27',
                      'source_line' => {},
                      'target_line' => '001S TR'
                      },

                      But the Actually Hash is in this format:

                      {
                      filename' => 'BRANCH27',
                      line_number' => '21',
                      'source_line' => {},
                      'target_line' => '001S TR'
                      }
                      That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying.
                      Code:
                      @keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
                      foreach $k (keys %hash) {
                      print "\n\n$k \n\n";
                       foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
                        print "$_   : $a->{$_}\n" foreach(@keys); ## use @keys here
                        print "\n";
                         }
                      }
                      You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.

                      Comment

                      • dillipkumar
                        New Member
                        • Mar 2008
                        • 41

                        #12
                        Originally posted by nithinpes
                        That's typically the behaviour of hash, the order won't be maintained. If you want to retain to retain the order, you can pass the order of keys into an array and use it while displaying.
                        Code:
                        @keys=('filename','line_number','source_line','target_line'); ## use the order of your choice
                        foreach $k (keys %hash) {
                        print "\n\n$k \n\n";
                         foreach $a (sort {$a->{'line_number'} <=> $b->{'line_number'}} @{$hash{$k}}) {
                          print "$_   : $a->{$_}\n" foreach(@keys); ## use @keys here
                          print "\n";
                           }
                        }
                        You would not have had this question if you had gone through Tie::SortHash that KevinADC mentioned in his reply.
                        Thank Nitin for ur help...

                        Comment

                        • eWish
                          Recognized Expert Contributor
                          • Jul 2007
                          • 973

                          #13
                          dillipkumar,

                          Four out of the the five posts that you have made in this tread you have neglected to use the code tags. Please use the code tags when posting code and data samples on this site.

                          &#91;CODE]...your code goes here...&#91;/CODE]

                          Thank You!

                          --Kevin

                          Comment

                          Working...