Sorting the sub-hashes gets a bit messy. I know that it violates your problem statement, but in the interest of simplicity, I would suggest just making an AoA structure at the same time as the hash. The AoA is a lot easier to sort. This does basically double the storage required, but the payback in simplicity is a lot.

I simplified your parsing a bit below. And I show my suggested AoA sorted 2 different ways. Perl is very good at sorting and many problems become easier with a sort of something or another. If you just need a printout grouped by "ch", then sorting is a good way. I guess a lot depends upon why you are using a hash and what you are doing with the data, which are things we just don't know. Trading off using more memory for clarity can be a very good decision. Unless these structures are huge, the memory usage may not matter even if you keep both representations.

#!/usr/bin/perl -w use strict; use Data::Dumper; my %channel_db_files = (); my @AoAsuggestion; # just a suggestion my $RMAN_NO_OF_CHANNELS = 3; # not clear why this is needed? my $RMAN_RUN_CH_NAME = 'ch'; my $current_ch; while (<DATA>) { $current_ch = $1 if /\s$RMAN_RUN_CH_NAME(\d+)/; my ($num,$name)= $_ =~ /^input datafile.*?number=(\d+).*?name=(.*)$ +/; if (defined $name) # match succeeded! # $num is defined if $name is defined { # your struct: $channel_db_files{"$RMAN_RUN_CH_NAME$current_ch"}{$num}=$name; #suggestion: push @AoAsuggestion,["$RMAN_RUN_CH_NAME$current_ch",$num+0,$name +]; } } print Dumper \%channel_db_files; print "File#\tFile Name\n"; @AoAsuggestion = sort { $a->[1] <=> $b->[1] }@AoAsuggestion; foreach my $lineref (@AoAsuggestion) { print "$lineref->[1]\t\t$lineref->[2]\n"; } # sort by channel, here just alpha sort, but probably want to split # out the number and do numeric sort on that if more than 10 channels print "\nSorting by channel, file number \n"; @AoAsuggestion = sort { $a->[0] cmp $b->[0] or $a->[1] <=> $b->[1] }@AoAsuggestion; my $spacerTag = $AoAsuggestion[0]->[0]; #for grouping by channel + foreach my $lineref (@AoAsuggestion) { if ($lineref->[0] ne $spacerTag) { print "\n"; $spacerTag = $lineref->[0]; } print "$lineref->[0]\t$lineref->[1]\t$lineref->[2]\n"; } =output $VAR1 = { 'ch2' => { '00003' => '/foo/oradata/bar/tools01.dbf', '00006' => '/foo/oradata/bar/foodb-index11.dbf', '00002' => '/foo/oradata/bar/undotbs01.dbf' }, 'ch1' => { '00010' => '/foo/oradata/bar/foodb-lob01.dbf', '00004' => '/foo/oradata/bar/foodb-data02.dbf', '00007' => '/foo/oradata/bar/undotbs02.dbf' }, 'ch3' => { '00005' => '/foo/oradata/bar/xml01.dbf', '00009' => '/foo/oradata/bar/foodb-index01.dbf', '00001' => '/foo/oradata/bar/system01.dbf', '00008' => '/foo/oradata/bar/foodb-data01.dbf' } }; File# File Name 1 /foo/oradata/bar/system01.dbf 2 /foo/oradata/bar/undotbs01.dbf 3 /foo/oradata/bar/tools01.dbf 4 /foo/oradata/bar/foodb-data02.dbf 5 /foo/oradata/bar/xml01.dbf 6 /foo/oradata/bar/foodb-index11.dbf 7 /foo/oradata/bar/undotbs02.dbf 8 /foo/oradata/bar/foodb-data01.dbf 9 /foo/oradata/bar/foodb-index01.dbf 10 /foo/oradata/bar/foodb-lob01.dbf Sorting by channel, file number ch1 4 /foo/oradata/bar/foodb-data02.dbf ch1 7 /foo/oradata/bar/undotbs02.dbf ch1 10 /foo/oradata/bar/foodb-lob01.dbf ch2 2 /foo/oradata/bar/undotbs01.dbf ch2 3 /foo/oradata/bar/tools01.dbf ch2 6 /foo/oradata/bar/foodb-index11.dbf ch3 1 /foo/oradata/bar/system01.dbf ch3 5 /foo/oradata/bar/xml01.dbf ch3 8 /foo/oradata/bar/foodb-data01.dbf ch3 9 /foo/oradata/bar/foodb-index01.dbf =cut __DATA__ Starting backup at 2011-05-31 02:00:05 channel ch1: starting compressed full datafile backup set channel ch1: specifying datafile(s) in backup set input datafile file number=00010 name=/foo/oradata/bar/foodb-lob01.dbf input datafile file number=00004 name=/foo/oradata/bar/foodb-data02.db +f input datafile file number=00007 name=/foo/oradata/bar/undotbs02.dbf channel ch1: starting piece 1 at 2011-05-31 02:00:06 channel ch2: starting compressed full datafile backup set channel ch2: specifying datafile(s) in backup set input datafile file number=00003 name=/foo/oradata/bar/tools01.dbf input datafile file number=00006 name=/foo/oradata/bar/foodb-index11.d +bf input datafile file number=00002 name=/foo/oradata/bar/undotbs01.dbf channel ch2: starting piece 1 at 2011-05-31 02:00:06 channel ch3: starting compressed full datafile backup set channel ch3: specifying datafile(s) in backup set input datafile file number=00008 name=/foo/oradata/bar/foodb-data01.db +f input datafile file number=00009 name=/foo/oradata/bar/foodb-index01.d +bf input datafile file number=00005 name=/foo/oradata/bar/xml01.dbf input datafile file number=00001 name=/foo/oradata/bar/system01.dbf channel ch3: starting piece 1 at 2011-05-31 02:00:07 channel ch1: finished piece 1 at 2011-05-31 02:34:54 #----------------8<----------------

In reply to Re: How-to sort nested hash table? by Marshall
in thread How-to sort nested hash table? by Scottie

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.