Mehta, Perdeep wrote: > Hi, > > I am looking for help to retrieve from a Hash of Hashes in insertion > order? I am not an expert in multi-dimensional Hashes, how could I use > Tie::IxHash module with a sub-Hash, if I could? > > Here is an example, I want to retrieve from sub-hash > %{$dir_contents->{$item} in the insertion order. I want to first print > key and value for “template_id”, then “trace_name”, then “trace_file”, > and so on… > > foreach my $item (keys %$dir_contents ) { > > print "$item\n"; > > foreach my $k (keys %{$dir_contents->{$item}} ) { > > print " $k => $dir_contents->{$item}{$k}\n"; > > }
Easiest way would be to just specify the order in $k you want: foreach my $k ('template_id', 'trace_name', 'trace_file', 'trace_format', 'trace_end') { print " $k => $dir_contents->{$item}{$k}\n"; } You could also prepend a number to each sub-hash name ('00-template_id') and then sort on the number or even replace the sub-hash names with ordered numbers (00, 01, 02 ...) and replace later using a hash to correlate the number to a name. ( 00 => 'template_id', ... ) > } > > > Here is sub that I am using to build the HoH: > > my %trace_info = (); > > while( my $f = readdir( DIR )) { > > $count++; > > next if( $f eq '.' or $f eq '..' ); > > my($template) = ($f =~ /(\d+[A-Z][A-Z]*)[0-9]*/); > > my($tracename) = ($f =~ /\d+([A-Z][A-Z]*[0-9]*)[a-z]/); > > … > > … > > $trace_info{$count}{'template_id'} = $template; > > $trace_info{$count}{'trace_name'} = $tracename; > > $trace_info{$count}{'trace_file'} = $tracefile; > > $trace_info{$count}{'trace_format'} = $traceformat; > > $trace_info{$count}{'trace_end'} = $tracedirection; > > } > > return \%trace_info; _______________________________________________ Perl-Unix-Users mailing list Perl-Unix-Users@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs