I have a little program that iterates through a HoH and builds a new hash from data found in the original. The program is working but it is pretty ugly and I would like input from others on ways to clean it up. One big issue that I don't like is in the 4th line
@holdval = keys %{$$switch_hash{$key}};

There is only one key in that level of the hash I am working on and that will never change. So I feel 'safe' in assuming the the 'keys' will only return one key. but this bothers me. Any suggestions?

Anyway, following is the original hash that is passed into my program, the program itself, the original hash after it has been changed and the hash that was created from data in the original hash.

The original hash'es final entry, if populated, contains a number:text. This is hours:reason. NOTE: I always 'use strict' and -w.

my %reasons; my ($key, $val, @holdval, $holdkey); while (($key, $val) = (each %{$switch_hash})){ @holdval = keys %{$$switch_hash{$key}}; for (values%{$val}){ while ( (my $tmpkey, my $tmpval) = each%{$_}){ my $newkey = $key.":".$tmpkey; my $newval =$tmpval; $tmpval =~ s/^\d+://; $reasons{$newkey} = $tmpval if ($tmpval ne ''); $newval =~ s/:.*$//; $$switch_hash{$key}{$holdval[0]}{$tmpkey} = $newval; } } }
Original Hash
$VAR1 = { 'Washington' => { 'uslecwas5e1' => { '01-AUG-2002' => '' } } +, 'Charleston' => { 'uslecchst5e1' => { '01-AUG-2002' => '' } +}, 'Richmond' => { 'uslecric5e1' => { '01-AUG-2002' => '' } }, 'West Palm Beach' => { 'uslecwpb5e1' => { '01-AUG-2002' => ' +' } }, 'Atlanta' => { 'uslecatl5e1' => { '14-AUG-2002' => '10:reaso +n', '15-AUG-2002' => '11:new reason' } }, 'Fort Myers' => { 'uslecftm5e1' => { '01-AUG-2002' => '' } } +, 'Mobile' => { 'uslecmob5e1' => { '01-AUG-2002' => '' } }, 'Nashville' => { 'uslecnas5e1' => { '01-AUG-2002' => '' } }, 'Orlando' => { 'uslecorl5e1' => { '12-AUG-2002' => '2:differ +ent reason' } }, 'Charlotte' => { 'uslechar5e1' => { '01-AUG-2002' => '' } }, 'Louisville' => { 'usleclou5e1' => { '01-AUG-2002' => '' } } +, 'Memphis' => { 'uslecmem5e1' => { '01-AUG-2002' => '' } }, 'Philadelphia' => { 'uslecphi5e1' => { '01-AUG-2002' => '' } + }, 'Chattanooga' => { 'uslecchat5e1' => { '01-AUG-2002' => '' } + }, 'Birmingham' => { 'uslecbir5e1' => { '01-AUG-2002' => '23:ju +nk' } }, 'Greensboro' => { 'uslcgb5e2sm' => { '01-AUG-2002' => '' } } +, 'New Orleans' => { 'uslecnew5e1' => { '01-AUG-2002' => '22:f +ake' } }, 'Jacksonville' => { 'uslecjac5e1' => { '01-AUG-2002' => '' } + }, 'Norfolk' => { 'uslecnor5e1' => { '01-AUG-2002' => '' } }, 'DEX' => { 'chrdex' => { '13-AUG-2002' => '1:some stuff', '1 +9-AUG-2002' => '1:reason again' } }, 'Atlanta II' => { 'uslecat25e1' => { '01-AUG-2002' => '' } } +, 'Baltimore' => { 'uslecbal5e1' => { '01-AUG-2002' => '' } }, 'Raleigh' => { 'uslecral5e1' => { '01-AUG-2002' => '' } }, 'Pittsburgh' => { 'uslecpit5e1' => { '01-AUG-2002' => '' } } +, 'Miami' => { 'uslecmia5e1' => { '01-AUG-2002' => '' } }, 'Knoxville' => { 'uslecknxv5e' => { '01-AUG-2002' => '' } }, 'Tampa' => { 'uslectam5e1' => { '01-AUG-2002' => '' } } };
New Hash
$VAR1 = { 'DEX:19-AUG-2002' => 'reason again', 'Atlanta:14-AUG-2002' => 'reason', 'Orlando:12-AUG-2002' => 'different reason', 'Birmingham:01-AUG-2002' => 'junk', 'DEX:13-AUG-2002' => 'some stuff', 'Atlanta:15-AUG-2002' => 'new reason', 'New Orleans:01-AUG-2002' => 'fake' };
Modified Original Hash
$VAR1 = { 'Washington' => { 'uslecwas5e1' => { '01-AUG-2002' => '' } } +, 'Charleston' => { 'uslecchst5e1' => { '01-AUG-2002' => '' } +}, 'Richmond' => { 'uslecric5e1' => { '01-AUG-2002' => '' } }, 'West Palm Beach' => { 'uslecwpb5e1' => { '01-AUG-2002' => ' +' } }, 'Atlanta' => { 'uslecatl5e1' => { '14-AUG-2002' => '10', '15 +-AUG-2002' => '11' } }, 'Fort Myers' => { 'uslecftm5e1' => { '01-AUG-2002' => '' } } +, 'Mobile' => { 'uslecmob5e1' => { '01-AUG-2002' => '' } }, 'Nashville' => { 'uslecnas5e1' => { '01-AUG-2002' => '' } }, 'Orlando' => { 'uslecorl5e1' => { '12-AUG-2002' => '2' } }, 'Charlotte' => { 'uslechar5e1' => { '01-AUG-2002' => '' } }, 'Louisville' => { 'usleclou5e1' => { '01-AUG-2002' => '' } } +, 'Memphis' => { 'uslecmem5e1' => { '01-AUG-2002' => '' } }, 'Philadelphia' => { 'uslecphi5e1' => { '01-AUG-2002' => '' } + }, 'Chattanooga' => { 'uslecchat5e1' => { '01-AUG-2002' => '' } + }, 'Birmingham' => { 'uslecbir5e1' => { '01-AUG-2002' => '23' } + }, 'Greensboro' => { 'uslcgb5e2sm' => { '01-AUG-2002' => '' } } +, 'New Orleans' => { 'uslecnew5e1' => { '01-AUG-2002' => '22' +} }, 'Jacksonville' => { 'uslecjac5e1' => { '01-AUG-2002' => '' } + }, 'Norfolk' => { 'uslecnor5e1' => { '01-AUG-2002' => '' } }, 'DEX' => { 'chrdex' => { '13-AUG-2002' => '1', '19-AUG-2002' + => '1' } }, 'Atlanta II' => { 'uslecat25e1' => { '01-AUG-2002' => '' } } +, 'Baltimore' => { 'uslecbal5e1' => { '01-AUG-2002' => '' } }, 'Raleigh' => { 'uslecral5e1' => { '01-AUG-2002' => '' } }, 'Pittsburgh' => { 'uslecpit5e1' => { '01-AUG-2002' => '' } } +, 'Miami' => { 'uslecmia5e1' => { '01-AUG-2002' => '' } }, 'Knoxville' => { 'uslecknxv5e' => { '01-AUG-2002' => '' } }, 'Tampa' => { 'uslectam5e1' => { '01-AUG-2002' => '' } } };

Edited: ~Wed Sep 11 23:05:05 2002 (GMT) by footpad: Added <readmore> tag and removed extraneous \n's to improve readability.


In reply to 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.