I have a hash of a hash that I would like to accumulate 'matching' keys with (if that makes sense?). Basically I am reading in some files to gather data and attempt to create some totals. The following code gets my HoH created but I’m not sure how to lookup matching sub keys in the second layer. The HoH basically looks like the following and I would like to add the 'value' when the 'second?' key matches. So $VAR2 contains '01:4928' in the first hash and $VAR2 contains the same key '01:4928' add the corresponding values for those keys. Please forgive the ugly code. Thanks in advance
$VAR1 = 'UE_RTB_H2H_TPS_20040127.DAT'; $VAR2 = { '01:4928' => '00', '00:4355' => '00', '04:4060' => '00', '03:2142' => '00', '02:3251' => '00', $VAR1 = 'UE_RTB_H2H_TPS_20040127.DAT'; $VAR2 = { '01:4928' => '01', '00:4355' => '02', '04:4060' => '00', '03:2142' => '01', '02:3251' => '00', '01:404' => '00', '00:3020' => '00', '00:2237' => '00', '00:3020' => '00', '00:2237' => '00', foreach $current_tps_file (sort readdir(TPS)) { my %total_hash; if ($current_tps_file =~ /^$args{b}$args{n}\w+$tps_pre_date_name$arg +s{d}$args{p}/) { # OK got a file that meets the match criteria above. open (TPS_FILE, "$current_tps_file") || die "FILE NOT OPEN: $!"; print "READING: $current_tps_file ...\n"; my @lines=<TPS_FILE>; # each @line array contains 1 minute. shift @lines; foreach my $line (@lines[1..$#lines]) { my @minute = (split (/ /, $line) ); my $current_time = $minute[0]; shift @minute; pop @minute; my $second = 0; foreach my $count (@minute){ $second++; my $key = $current_time . $second; #print "$key"; $total_hash{$key} = $count; $all_files{$current_tps_file} = \%total_hash; } } } close (TPS_FILE); } closedir (TPS); print Dumper(%all_files);
UPDATE: basically I would like to be able to search for common keys like below but in a HoH instead of two different hashes?
my @common = (); foreach (keys %hash1) { push(@common, $_) if exists $hash2{$_}; }

BazB: use vanilla single quotes


In reply to HoH and accumulation by djbiv

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.