By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,834 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,834 IT Pros & Developers. It's quick & easy.

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

P: 9
Hi,
I have a hash in hash file in perl which looks some thing like this:
Expand|Select|Wrap|Line Numbers
  1. $FIELDS = {
  2.           'abc' => {
  3.         'Description' => {
  4.                             'Purpose' => 'some data is present',
  5.                             'Background' => 'something',
  6.                 }
  7.                  'Run Instructions' => {
  8.                             'Data' => 'something',
  9.                             'DEFAULT.INITIAL_WIPE' => 'somethng',
  10.                             'DEFAULT.SC_ON' => 'somethng',
  11.                  }
  12.                  'File' => '1',
  13.                  'Category => 'somethng',
  14.           },
  15.      'xyz' => {
  16.         'Description' => {
  17.                             'Purpose' => 'some data is present',
  18.                             'Background' => 'something',
  19.                 }
  20.                  'Run Instructions' => {
  21.                             'Data' => 'something',
  22.                             'DEFAULT.INITIAL_WIPE' => 'somethng',
  23.                             'DEFAULT.SC_ON' => 'somethng',
  24.                  }
  25.                  'File' => '2',
  26.                  'Category => 'somethng',
  27.           },
  28.           ............so on and so forth      
  29.       };
I want to display the values present in the inner hash i.e 'Description' and 'Run Instructions'.
I start off with somethng like this:
Expand|Select|Wrap|Line Numbers
  1. %mydata = $FIELDS;
  2. foreach my $key (sort {$myData{$a}{Category} cmp $myData{$b}{Category} or
  3.                       $myData{$a}{File} cmp $myData{$b}{File}} keys %myData) {
  4. .....................
  5. ..................
  6.       if ($myData{$key}{'Description'}){
  7.             print "<br><b>Description:</b><br>";
  8.             my $counterdesc = $myData{$key}{'Description'};
  9.             %desc = %$counterdesc;
  10.             while (($keydatadesc,$valuedatadesc) = each(%desc)) {
  11.                 if ($keydatadesc =~ /^Data$/) {
  12.                     print $valuedatadesc."<br>";
  13.                 }
  14.             }
  15.             foreach my $keydesc (keys %desc) {
  16.                 if ($keydesc !~ /^Data$/) {
  17.                     print "<font   
  18.                                         color=blue><b>$keydesc:</b></font><br>";
  19.                     if ($desc{$keydesc} !~ /^$/) {
  20.                         print $desc{$keydesc}."<br>";
  21.                     }
  22.                     else {
  23.                         print $desc{$keydesc}."<br><br>";
  24.                     }
  25.                 }
  26.             }
  27.         }
  28. ......Somethng for Run Instruction also
  29. .....
  30. }
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...
Nov 14 '08 #1
Share this Question
Share on Google+
5 Replies


KevinADC
Expert 2.5K+
P: 4,059
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.
Nov 14 '08 #2

P: 9
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..
Nov 14 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
When you want help with your code, post it.
Nov 14 '08 #4

P: 9
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!!!!
Nov 15 '08 #5

KevinADC
Expert 2.5K+
P: 4,059
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.
Nov 15 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.