Your data:

Your data structure should reflect that.

use strict; use warnings; my %data; my %dates; my %zips; while (<DATA>) { chomp; my ($zip, $amt, $date, $candid) = split; $zips {$zip } = 1; $dates{$date} = 1; $data{$candid}{$zip}{amts}{$date} += $amt; } my @candids = sort keys %data; my @dates = sort keys %dates; my @zips = sort keys %zips; local $, = "\t"; local $\ = "\n"; for my $candid (@candids) { my $table = $data{$candid}; print('CANDID', 'ZIP', (map "MONEY ($_)", @dates)); for my $zip (@zips) { my $row = $table->{$zip}; my @amts = map { $_ || 0 } @{ $row->{amts} }{ @dates }; print($candid, $zip, @amts); } print(''); } __DATA__ 12345 200 11062000 C1234 12345 50 11062000 C1234 67890 50 11072000 D5555 38401 250 11062000 C00003418 77024 200 11062000 C00003418 75711 1000 11072000 C00003418 33480 5000 11072000 C00003418
CANDID ZIP MONEY (11062000) MONEY (11072000) C00003418 12345 0 0 C00003418 33480 0 5000 C00003418 38401 250 0 C00003418 67890 0 0 C00003418 75711 0 1000 C00003418 77024 200 0 CANDID ZIP MONEY (11062000) MONEY (11072000) C1234 12345 250 0 C1234 33480 0 0 C1234 38401 0 0 C1234 67890 0 0 C1234 75711 0 0 C1234 77024 0 0 CANDID ZIP MONEY (11062000) MONEY (11072000) D5555 12345 0 0 D5555 33480 0 0 D5555 38401 0 0 D5555 67890 0 50 D5555 75711 0 0 D5555 77024 0 0

A minor change would cut out "blank" rows and columns if that's what you prefer. Specifically, collect a lists of zips and dates per cand instead one one global list.


In reply to Re: Creating One Table From Several Hashes by ikegami
in thread Creating One Table From Several Hashes by bryced1234

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.