how to get the keys in a hash file in the same order?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nirmal1349
    New Member
    • Sep 2008
    • 9

    #1

    how to get the keys in a hash file in the same order?

    Hi,
    I have a hash in hash file in perl which looks some thing like this:
    Code:
    $FIELDS = {
              'abc' => {
    		'Description' => {
                                'Purpose' => 'some data is present',
                                'Background' => 'something',
                    }
                     'Run Instructions' => {
                                'Data' => 'something',
                                'DEFAULT.INITIAL_WIPE' => 'somethng',
                                'DEFAULT.SC_ON' => 'somethng',
                     }
                     'File' => '1',
                     'Category => 'somethng',
              },
    	 'xyz' => {
    		'Description' => {
                                'Purpose' => 'some data is present',
                                'Background' => 'something',
                    }
                     'Run Instructions' => {
                                'Data' => 'something',
                                'DEFAULT.INITIAL_WIPE' => 'somethng',
                                'DEFAULT.SC_ON' => 'somethng',
                     }
                     'File' => '2',
                     'Category => 'somethng',
              },
              ............so on and so forth      
          };
    I want to display the values present in the inner hash i.e 'Description' and 'Run Instructions'.
    I start off with somethng like this:
    Code:
    %mydata = $FIELDS;
    foreach my $key (sort {$myData{$a}{Category} cmp $myData{$b}{Category} or
                          $myData{$a}{File} cmp $myData{$b}{File}} keys %myData) {
    .....................
    ..................
          if ($myData{$key}{'Description'}){
                print "<br><b>Description:</b><br>";
    			my $counterdesc = $myData{$key}{'Description'};
    			%desc = %$counterdesc;
    			while (($keydatadesc,$valuedatadesc) = each(%desc)) {
    				if ($keydatadesc =~ /^Data$/) {
    					print $valuedatadesc."<br>";
    				}
    			}
    			foreach my $keydesc (keys %desc) {
    				if ($keydesc !~ /^Data$/) {
    					print "<font   
                                            color=blue><b>$keydesc:</b></font><br>";
    					if ($desc{$keydesc} !~ /^$/) {
    						print $desc{$keydesc}."<br>";
    					}
    					else {
    						print $desc{$keydesc}."<br><br>";
    					}
    				}
    			}
            }
    ......Somethng for Run Instruction also
    .....
    }
    The problem is that while I run the loop to display the items present in 'Description' I dont get the keys in the order they are present in the hash file i.e if for the hash 'abc' ,I want 'Purpose' first followed by 'Background'... But while sorting the order gets shuffled...and I end up getting the information but the keys are shuffled..I also dont want to sort it as that does not fulfill my requirement.
    I want the keys present in the inner hash exactly in the same order as it is present in the file..

    Please reply as soon as possible...
    Last edited by eWish; Nov 14 '08, 11:13 PM. Reason: Please use the code tags
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    There is no urgent help here so in the future don't ask for it.

    If you want to maintain order, use an array to store the hash keys you want to display in a certain order.

    Comment

    • nirmal1349
      New Member
      • Sep 2008
      • 9

      #3
      Hi Kevin,
      I tried doin it in the array...But stil the problem did not resolve..I figured out tht there is some problem while I dereference the hash 'Description'.M working on that..

      Thanks..

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        When you want help with your code, post it.

        Comment

        • nirmal1349
          New Member
          • Sep 2008
          • 9

          #5
          Hey kevin,
          Thanks for the reply.Next time I shall post the code...
          I got the solution for the problem...
          Thnks again for the help!!!!

          Comment

          • KevinADC
            Recognized Expert Specialist
            • Jan 2007
            • 4092

            #6
            Originally posted by nirmal1349
            Hey kevin,
            Thanks for the reply.Next time I shall post the code...
            I got the solution for the problem...
            Thnks again for the help!!!!
            Well, considering I was no help to you that I can see, you're very welcome.

            Comment

            Working...