data67 has asked for the wisdom of the Perl Monks concerning the following question:

PROBLEM
I am trying to display a report with the use of hashes.
I am pulling in data from a flat-file which has customer
records that have a Fiscal Year date and trying to match it with some dates to get a combined result.

DESCRIPTION
Lets say my hash looks like this after i set it up:

%HoH = ( cat => { 'FY2003-Q2' => { 'this is the customers data => '1' }, 'FY2003-Q4' => { 'this is the customers data => '1' }, 'FY2004-Q1' => { 'this is the customers data => '1' }, 'FY2004-Q2' => { 'this is the customers data => '1' }, }, dog => { 'FY2003-Q2' => { 'this is the customers data => '1' }, 'FY2003-Q1' => { 'blah blah blah' => '1' }, }, catdog => { 'FY2004-Q1' => { 'blah blah blah' => '1' }, 'FY2004-Q2' => { 'blah blah blah' => '1' } } );

Now one of my subs takes the current date and gives me the next four quarters and i use these 5 dates to sort my data like:
my %date_slots = ( 1 => FY2003-Q2, 2 => FY2003-Q3, 3 => FY2003-Q4, 4 => FY2004-Q1, 5 => FY2004-Q2 );

Now once i got all my data and dates setup i have to print the them out. so far so good.
BUT for those dates that i dont have anything i have to print "no data". See here:
foreach $customer (keys %HoH) { print "$customer\n"; foreach $date (keys %{$HoH{$customer}} ) { print "\t\t$date"; foreach my $data (keys %{$cal{$customer}{$date}} ) { print " = $data\n"; } } }
This prints what ever is in my HoH hash.

cat FY2003-Q2 = blah blah blah FY2003-Q4 = blah blah blah FY2004-Q1 = blah blah blah FY2004-Q2 = blah blah blah dog FY2003-Q1 = blah blah blah FY2003-Q2 = blah blah blah catdog FY2004-Q1 = blah blah blah FY2004-Q2 = blah blah blah
But my goal is to take the %date_slots hash and match my HoH data
with it if no match then print "no data". something like:
cat FY2003-Q2 = blah blah blah FY2003-Q3 = no data FY2003-Q4 = blah blah blah FY2004-Q1 = blah blah blah FY2004-Q2 = blah blah blah dog FY2003-Q2 = blah blah blah FY2003-Q3 = no data FY2003-Q4 = no data FY2004-Q1 = no data FY2004-Q2 = no data catdog FY2003-Q2 = no data FY2003-Q3 = no data FY2003-Q4 = no data FY2004-Q1 = blah blah blah FY2004-Q2 = blah blah blah

Sorry for the lousy explanation, and thanks for your help.

"Learning is a treasure
that will follow its owner everywhere."
-Chinese proverb

Replies are listed 'Best First'.
Re: Mix and Match Hashes
by hiseldl (Priest) on Oct 03, 2002 at 16:57 UTC

    Try iterating over %date_slots and printing the customer data || 'no data'; Example:

    #!perl -w %HoH = ( cat => { 'FY2003-Q2' => { 'this is the customers data' => '1' }, 'FY2003-Q4' => { 'this is the customers data' => '1' }, 'FY2004-Q1' => { 'this is the customers data' => '1' }, 'FY2004-Q2' => { 'this is the customers data' => '1' }, }, dog => { 'FY2003-Q2' => { 'this is the customers data' => '1' }, 'FY2003-Q1' => { 'blah blah blah' => '1' }, }, catdog => { 'FY2004-Q1' => { 'blah blah blah' => '1' }, 'FY2004-Q2' => { 'blah blah blah' => '1' } } ); my %date_slots = ( 1 => 'FY2003-Q2', 2 => 'FY2003-Q3', 3 => 'FY2003-Q4', 4 => 'FY2004-Q1', 5 => 'FY2004-Q2', ); foreach $customer (keys %HoH) { print "$customer\n"; foreach $slot (sort keys %date_slots) { $date = $date_slots{$slot}; print "\t\t$date = "; ($key, $val) = each %{$HoH{$customer}{$date}}; print $key || "no data"; print "\n"; } # foreach $date (keys %{$HoH{$customer}} ) { # print "\t\t$date"; # foreach my $data (keys %{$cal{$customer}{$date}} ) { # print " = $data\n"; # } # } } __END__ # my results: cat FY2003-Q2 = this is the customers data FY2003-Q3 = no data FY2003-Q4 = this is the customers data FY2004-Q1 = this is the customers data FY2004-Q2 = this is the customers data dog FY2003-Q2 = this is the customers data FY2003-Q3 = no data FY2003-Q4 = no data FY2004-Q1 = no data FY2004-Q2 = no data catdog FY2003-Q2 = no data FY2003-Q3 = no data FY2003-Q4 = no data FY2004-Q1 = blah blah blah FY2004-Q2 = blah blah blah

    Note: I had to fix up your %HoH and %date_slots hashes because they were missing ending quotes.

    --
    hiseldl
    What time is it? It's Camel Time!

Re: Mix and Match Hashes
by Tanalis (Curate) on Oct 03, 2002 at 17:02 UTC
    With hashes, you can easily check to see if a key exists using the exists function. As the key does not exist in HoH hash, it should therefore be a simple matter to print "no data": (untested code)

    foreach my $quarter (keys %date_slots) { if (exists $HoH{$customer}{$quarter} { # print data } else { print " = no data"; } }

    Is only a quick reply .. hope it helps a little.
    --Foxcub

Re: Mix and Match Hashes
by zigdon (Deacon) on Oct 03, 2002 at 16:55 UTC

    Shouldn't you be looping on the keys of %date_slots instead of %{$HoH{$customer}}?

    foreach $customer (keys %HoH) { print "$customer\n"; foreach $date (keys %date_slots ) { print "\t\t$date"; foreach my $data (keys %{$cal{$customer}{$date}} ) { if (defined $data) { print " = $data\n"; } else { print " = no data\n"; } } } }

    -- Dan