You're using each, but throwing away the benefit of getting the value along with the key, and looking it up in the original hash instead, several times. There are several temporary variables you can get rid of, and unless you need them elsewhere (which I'd be wary of), the scope for your essential variables can be kept much narrower. Personal preference of mine: use fewer brackets and more spaces instead.
my %reasons; while (my($city, $code_info) = each %$switch_hash) { my ($code, $date_info) = %$code_info; while(my ($date, $reason) = each %$date_info) { next unless $reason =~ /^(\d+):(.*)/; $date_info->{$date} = $1; $reasons{"$city:$date"} = $2; } }
If you're sure the hash will only have one entry, you can store the code along with the name in the toplevel hash key and save yourself a lot of dereferencing:
my $switch_hash = { 'Washington:uslecwas5e1' => { '01-AUG-2002' => '' }, 'Charleston:uslecchst5e1' => { '01-AUG-2002' => '' }, 'Richmond:uslecric5e1' => { '01-AUG-2002' => '' }, # ...
You could also store that code in an altogether separate hash elso keyed on the same city names. Or you could store it in a special key of the subhash that doesn't contain a valid date, like
my $switch_hash = { 'Washington' => { CODE => 'uslecwas5e1', '01-AUG-2002' => '' }, # ...
and write something like
my %reasons; while (my($city, $info) = each %$switch_hash) { while(my ($date, $reason) = each %$info) { next unless $reason =~ /^(\d+):(.*)/ and $date ne 'CODE'; $info->{$date} = $1; $reasons{"$city:$date"} = $2; } }

Makeshifts last the longest.


In reply to Re: newbie question on HoH manipulation by Aristotle
in thread newbie question on HoH manipulation by gnu@perl

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.