Here's a solution that sorts books numerically by book number and then case-insensitive alphabetically by the first 'word' found in the citation. You might want to use something like  \w+ or  [[:alnum:]]+ instead of  [[:alpha:]]+ in  by_first_word() if there might ever be a citation like "666, The Number of the Beast". The sorts are far from the fastest possible, but should be OK if you have no more than a few score books of a few thousand citations each. (After all, how often will you do this sort?)

>perl -wMstrict -le "sub printIndex { my ($hr) = @_; for my $book (sort by_book_number keys %$hr) { for my $citation (sort by_first_word keys %{$hr->{$book}}) { print qq{'$book' '$citation' pgs }, join ', ', @{ $hr->{$book}{$citation} }; } } } ;; sub by_book_number { my ($an) = $a =~ m{ (\d+) }xms; my ($bn) = $b =~ m{ (\d+) }xms; return $an <=> $bn; } ;; sub by_first_word { my ($aw) = $a =~ m{ ([[:alpha:]]+) }xms; my ($bw) = $b =~ m{ ([[:alpha:]]+) }xms; return uc($aw) cmp uc($bw); } ;; my %ibIndex = ( 'Book 10' => { 'wormhole' => [ 150 ], 'World Wide Web (WWW)' => [ 75, 137, 153 ], 'Wigner, Eugene P. (1902-1995)' => [ 200, 200, 203, 208 ], 'World War II (WWII)' => [ 127 ], 'Work and play' => [ 235 ], }, 'Book 1' => { 'wormhole' => [ 266 ], 'Whistlefield Estate' => [ 64, 73, 74, 96 ], 'wisdom' => [ 134, 140, 161, 210, 240, 58 ], 'Yost, Bill' => [ 75, 88 ], '\"winners,\" evolutionary (see \"evolution,\" \"natural selectio +n\")' => [ 198, 199, 226, 2 ], 'Wigner, Eugene P. (1902-1995)' => [ 32 ], }, 'Book 2' => { 'worldview' => [ 343 ], 'wisdom' => [ 71, 130, 153, 220, 262, 265, 286, 313, 331 ], 'will' => [ 82, 117, 164 ], 'World community' => [ 344 ], }, ); ;; printIndex(\%ibIndex); " 'Book 1' 'Whistlefield Estate' pgs 64, 73, 74, 96 'Book 1' 'Wigner, Eugene P. (1902-1995)' pgs 32 'Book 1' '"winners," evolutionary (see "evolution," "natural selection +")' pgs 198, 199, 226, 2 'Book 1' 'wisdom' pgs 134, 140, 161, 210, 240, 58 'Book 1' 'wormhole' pgs 266 'Book 1' 'Yost, Bill' pgs 75, 88 'Book 2' 'will' pgs 82, 117, 164 'Book 2' 'wisdom' pgs 71, 130, 153, 220, 262, 265, 286, 313, 331 'Book 2' 'World community' pgs 344 'Book 2' 'worldview' pgs 343 'Book 10' 'Wigner, Eugene P. (1902-1995)' pgs 200, 200, 203, 208 'Book 10' 'Work and play' pgs 235 'Book 10' 'World Wide Web (WWW)' pgs 75, 137, 153 'Book 10' 'World War II (WWII)' pgs 127 'Book 10' 'wormhole' pgs 150

In reply to Re: How do I sort a hash in a hash? by AnomalousMonk
in thread How do I sort a hash in a hash? by dbmathis

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.