Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Hashes of Hash in perl

by Rajsai2825 (Novice)
on Apr 17, 2013 at 09:39 UTC ( #1029107=note: print w/replies, xml ) Need Help??


in reply to Perl Hash

Hi, could you please help on this? MY Code:
$fulllookupKey = $field[1]."\t". $field[2]."\t". $field[3]."\ +t".$field[4]."\t".$field[5]; $RescatKey = $field[1]; $RessubcatKey = $field[2]; $MasterSrkey = $field[3]; $field[4] =~ s/^\s+//; $field[21] =~ s/^\s+//; $Count = $field[6]; $Amount = $field[7]; if (exists($output{$RescatKey}{$RessubcatKey}{$fulllookupKey})) + { $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[0] += $fie +ld[6]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[1] += $fie +ld[7]; } else { $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[0] = $fiel +d[6]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[1] = $fiel +d[7]; } } for my $k1 (sort keys %output){ print "$k1\n"; for my $k2 ( sort keys %{ $output{$k1} } ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $output{$k1}{$k2} } ) { my $Source = substr($k3,16,100); my $Source1 = substr($k3,16,07); print "\t\t$Source $output{$k1}{$k2}{$k3}->[0]\t$output{$k1}{$ +k2}{$k3}->[1]\n"; print "\t\t$Source1\t TOTAL \t$output{$k1}{$k2}{$k3}->[1]\t$outpu +t{$k1}{$k2}{$k3}->[0]\n"; } } }
INPUT is:
<code> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 10.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 14.00 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 10.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 14.00
</code> OUTPUT expected is:
Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 2 44.32 EMC CHECK KBCDEFGHI AZ_checker 2 24.00 EMC CHECK TOTAL 4 64.32 EMC CHECK1 abcdefghi AZ_checker 2 44.59 EMC CHECK1 HGIJHKKIF AZ_checker 2 24.00 EMC CHECK1 TOTAL 4 68.59 Detial_Summary TOTAL 8 132.91 Summary TOTAL 8 132.91

Replies are listed 'Best First'.
Re: Hashes of Hash in perl
by hdb (Monsignor) on Apr 17, 2013 at 12:04 UTC

    How about this? Still lots of open questions...

    I have ignored the first 3 columns as they do not seem to make any difference (even though they have different entries, these differences are ignored in your desired output). In order to check whether columns line up properly, open in Excel with TAB delimited.

    use strict; use warnings; my %output; my %total; while (my $line = <DATA>){ chomp $line; my @field = split /\s+/, $line; # fields 0, 1, 2 do seem to be required $output{$field[3]}{$field[4]}{$field[5]}{COUNT} += $field[6]; $output{$field[3]}{$field[4]}{$field[5]}{VALUE} += $field[7]; $total{$field[3]}{COUNT} += $field[6]; $total{$field[3]}{VALUE} += $field[7]; $total{TOTAL}{COUNT} += $field[6]; $total{TOTAL}{VALUE} += $field[7]; } print "Summary\n"; print "\tDetailed Summary\n"; for my $k1 (sort keys %output){ for my $k2 ( sort keys %{ $output{$k1} } ) { for my $k3 ( sort keys %{ $output{$k1}{$k2} } ) { print "\t\tEMC\t$k1\t$k2\t$k3\t$output{$k1}{$k +2}{$k3}{COUNT}\t$output{$k1}{$k2}{$k3}{VALUE}\n"; } } print "\t\tEMC\t$k1\tTOTAL\t\t$total{$k1}{COUNT}\t$total{$k1}{ +VALUE}\n"; } print "\tDetailed Summary\tTOTAL\t\t$total{TOTAL}{COUNT}\t$total{TOTAL +}{VALUE}\n"; print "Summary\n"; __DATA__ Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 10.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 14.00 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 10.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 14.00
Re: Hashes of Hash in perl
by hdb (Monsignor) on Apr 17, 2013 at 09:51 UTC

    Is this post a continuation of this thread Perl Hash ?

      Yes

        I do not understand why you have not followed advice given in earlier thread but here is your code cleaned somewhat and running. Pls state what you want to achieve.

        use strict; use warnings; my %output; while (my $line = <DATA>){ chomp $line; my @field = split /\s+/, $line; my $fulllookupKey = $field[1]."\t". $field[2]."\t". $field[3]."\t +".$field[4]."\t".$field[5]; my $RescatKey = $field[0]; my $RessubcatKey = $field[1]; my $MasterSrkey = $field[2]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[0] += $field[ +6]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[1] += $field[ +7]; } for my $k1 (sort keys %output){ print "$k1\n"; for my $k2 ( sort keys %{ $output{$k1} } ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $output{$k1}{$k2} } ) { my $Source = substr($k3,15,100); print "\t\t$Source $output{$k1}{$k2}{$k3}->[0] +\t$output{$k1}{$k2}{$k3}->[1]\n"; print "\t\t$Source\t TOTAL \t$output{$k1}{$k2} +{$k3}->[1]\t$output{$k1}{$k2}{$k3}->[0]\n"; } } } __DATA__ Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 10.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 14.00 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 10.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 14.00
Re: Hashes of Hash in perl
by CountOrlok (Friar) on Apr 17, 2013 at 12:53 UTC
    Just one friendly piece of advice: Please stick to one convention when using variable names. You have some variables with like $Summary (a convention no one uses), some like $fulllookupKey (which should really have been $fullLookupKey) and I also saw something like $load_data in one of your post.

    Sticking to one convention helps you more than anyone else. You don't need to keep going back to check how you spelled that variable.

Re: Hashes of Hash in perl
by 2teez (Vicar) on Apr 17, 2013 at 14:39 UTC

    Hi Rajsai2825,
    I know you have been given a workable script, but I just want to point out that you can do all your "stuff" using just a hash and function unpack.
    As shown below. (ofcourse, I wouldn't drop all the "jar of honey" on your laps!:))

    use warnings; use strict; my %input; my $total; my $grand_total; while (<DATA>) { chomp; next if /^$/; my ( $k1, $k2, $k3, $k4, $k5 ) = unpack("x25 A10 A10 x1 A10 x1 A1 +x1 A5"); $total += $k4; $grand_total += $k5; push @{ $input{'summary'}{'Detial_Summary'}{$k1}{$k2}{$k3}{ ++$k4 +} }, $k5; } push @{ $input{'summary'}{'TOTAL'}{$total} }, $grand_total; use Data::Dump; dd \%input; __DATA__ Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 10.00 Summary Detial_Summary EMC CHECK KBCDEFGHI AZ_checker 1 14.00 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 10.00 Summary1 Detial_Summary1 EMC CHECK1 HGIJHKKIF AZ_checker 1 14.00

    OUTPUT
    { summary => { Detial_Summary => { "EMC CHECK" => { " abcdefghi" => { AZ_checker => { 2 => [20.32, + "24.00"] } }, " KBCDEFGHI" => { AZ_checker => { 2 => ["10.00 +", "14.00"] } }, }, "EMC CHECK1" => { " abcdefghi" => { AZ_checker => { 2 => [20.59, + "24.00"] } }, " HGIJHKKIF" => { AZ_checker => { 2 => ["10.00 +", "14.00"] } }, }, }, TOTAL => { 8 => [136.91] }, }, }
    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Hashes of Hash in perl
by Anonymous Monk on Apr 17, 2013 at 09:41 UTC
    Your code is incomplete, it doesn't read input
      Please find the subroutine:
      while (chomp(@field = split (/\t/, <FILE>))) { next unless ($field[0] =~ /^$USG_REC_IND/); foreach my $key ( keys %load_data ) { { if (length($key) == 0) { next; } } } $fulllookupKey = $field[1]."\t". $field[2]."\t". $field[3]."\t".$ +field[4]."\t".$field[5]; $RescatKey = $field[1]; $RessubcatKey = $field[2]; $MasterSrkey = $field[3]; $field[4] =~ s/^\s+//; $field[21] =~ s/^\s+//; $Count = $field[6]; $Amount = $field[7]; if (exists($output{$RescatKey}{$RessubcatKey}{$fulllookupKey})) { $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[0] += $fie +ld[6]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[1] += $fie +ld[7]; } else { $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[0] = $fiel +d[6]; $output{$RescatKey}{$RessubcatKey}{$fulllookupKey}->[1] = $fiel +d[7]; } } for my $k1 (sort keys %output){ print "$k1\n"; for my $k2 ( sort keys %{ $output{$k1} } ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $output{$k1}{$k2} } ) { my $Source = substr($k3,16,100); print "\t\t$Source $output{$k1}{$k2}{$k3}->[0]\t$output{$k1}{$k2} +{$k3}->[1]\n"; print "\t\t$Source1\t TOTAL \t$output{$k1}{$k2}{$k3}->[1]\t$outpu +t{$k1}{$k2}{$k3}->[0]\n"; } } } close (OUT); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029107]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2018-07-20 05:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (424 votes). Check out past polls.

    Notices?