Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: Perl Hash

by NetWallah (Abbot)
on Apr 17, 2013 at 04:31 UTC ( #1029050=note: print w/ replies, xml ) Need Help??


in reply to Perl Hash

Here is one way to do it:

use strict; use warnings; my %h; while (<DATA>){ chomp; next if length($_) < 30; my ($k1, $k2,$k3, $val) = unpack("A8 x1 A15 x1 A33 x1 A5", $_); $h{$k1}{$k2}{$k3} += $val; } for my $k1 (sort keys %h){ print "$k1\n"; for my $k2 ( sort keys %{ $h{$k1} } ) { print "\t$k2\n"; for my $k3 ( sort keys %{ $h{$k1}{$k2} } ) { print "\t\t$k3 $h{$k1}{$k2}{$k3}\n"; } } } __DATA__ Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 20.32 Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 24.00 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 20.59 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 24.00
prints:
Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 44.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 44.59

             "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
        -- Dr. Cox, Scrubs


Comment on Re: Perl Hash
Select or Download Code
Re^2: Perl Hash
by Rajsai2825 (Novice) on Apr 17, 2013 at 05:23 UTC
    Hi, Thanks a lot for the inputs. how can i ge this ouput for the same code. <DATA> Summary Detial_Summary EMC CHECK abcdefghi AZ_checker 1 44.32 EMC CHECK Grand_Total 1 44.32 Summary1 Detial_Summary1 EMC CHECK1 abcdefghi AZ_checker 1 44.59 EMC CHECK1 Grand_Total 1 44.59 </DATA>
      Use <code> tags, not <DATA>.

      To get the grand totals, add the following as the last line inside the WHILE loop:

      $h{$k1}{$k2}{+substr($k3,0,10) . " Grand Total"} += $val;
      This is a kludgy way to do it, but, given your sparse data definition, it does put out a grand total.

      A more robust implementation would require one more level of hashing - I leave that as an exercise for you to implement.

                   "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
              -- Dr. Cox, Scrubs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1029050]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2014-10-22 04:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls