Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Accumulating Column Total From a CSV for a Common Key Value

by agianni (Hermit)
on Aug 29, 2007 at 19:34 UTC ( #635919=note: print w/ replies, xml ) Need Help??


in reply to Accumulating Column Total From a CSV for a Common Key Value

Instead of:

$total{$appl} += $cpupct;

try:

$total{$server}->{$date}->{$appl} += $cpupct;

Then you'll just need to do three nested for loops to go through the three levels of the hash:

for my $server ( keys %total ){ for my $date ( keys %{$total{$server}} ){ for my $appl ( keys %{$total{$server}->{$date}} ){ printf $OUTCSV ("$server,$date,$appl,%3.1f%% \n", $total{$ +server}->{$date}->{$appl}); } } }

Update: code updated to correctly output total percentage.

Which will allow you to sum up the $cpupct by date and by server and will add those two columns to you output.

perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'


Comment on Re: Accumulating Column Total From a CSV for a Common Key Value
Select or Download Code
Re^2: Accumulating Column Total From a CSV for a Common Key Value
by GeneV1 (Initiate) on Aug 30, 2007 at 12:30 UTC

    agianni,


    I ran your modified code with the input that I sent
    initially. When I run the script I am getting the use
    of an uninitialized variable message at the printf
    statement.

    printf $OUTCSV ("$server,$date,$appl,%3.1f%% \n", $total{$appl});

    The $total($appl) must be wrong, as the output is as
    follows:
    xsd00544,05/2007,PTI - Private Client Services,0.0% xsd00544,05/2007,CTI,0.0% xsd00544,05/2007,OMS,0.0% xsd00544,05/2007,CTI Production ,0.0% xsd00544,05/2007,SSG-RAC-Maestro ,0.0% xsd00544,05/2007,SSG-IPM,0.0% xsd00544,05/2007,Merva,0.0% xsd00544,05/2007,"DB2DARI ""stored procedures"" prod",0.0% xsd00544,05/2007,SSG-PMD-Omegamon,0.0% xsd00544,05/2007,BMW,0.0%
      Sorry, that was just off the top of my head, not tested. Hopefully you've figured this out on your own already, but just replace $total{$appl} with $total{$server}->{$date}->{$appl} and that should give you the values in the pct column you're looking for. If you didn't figure that out on your own, you should really read up on perl data structures. perldsc is a good place to start.
      perl -e 'split//,q{john hurl, pest caretaker}and(map{print @_[$_]}(joi +n(q{},map{sprintf(qq{%010u},$_)}(2**2*307*4993,5*101*641*5261,7*59*79 +*36997,13*17*71*45131,3**2*67*89*167*181))=~/\d{2}/g));'

        agianni, this worked marvelously and most importantly I picked up a few pointers on perl use. It now turns out that I have in my input multiple Servers (the 1st field) in addition to multiple Login ID's (the 3rd field) per Application (the 4th field). I am able to accumulate the Percent of Totals by Application with your code.

        Sample output from your code would look as follows:

        xsd00501,05/2007,"Shared Production (PRD,0.2% xsd00501,05/2007,OMX (old MBX userid),0.9% xsd00501,05/2007,PTI - Private Client Services,0.0% xsd00501,05/2007,SSG-IPM,3.1% xsd00501,05/2007,SSG-PMD-Omegamon,0.6% xsd00501,05/2007,SSG-RAC-Maestro ,45.7% xsd00501,05/2007,STW - Stock Transfer Workstation,1.1% xsd00506,05/2007,ADR,0.4% xsd00506,05/2007,OBR Production,0.1% xsd00506,05/2007,RAC/AMOS/DSO,43.3% xsd00506,05/2007,SPS - Inform,1.2% xsd00506,05/2007,SPS Production (also IRD till 7/03),11.7% xsd00506,05/2007,SSG-IPM,2.6% xsd00506,05/2007,SSG-PMD-Omegamon,0.6% xsd00506,05/2007,SSG-RAC-Maestro ,17.0% xsd00544,05/2007,"DB2DARI ""stored procedures"" prod",2.1% xsd00544,05/2007,BMW,0.0% xsd00544,05/2007,CTI Production ,21.7% xsd00544,05/2007,CTI,13.1% xsd00544,05/2007,Merva,0.0% xsd00544,05/2007,OMS,13.7% xsd00544,05/2007,PTI - Private Client Services,0.0% xsd00544,05/2007,SSG-IPM,4.5% xsd00544,05/2007,SSG-PMD-Omegamon,0.1% xsd00544,05/2007,SSG-RAC-Maestro ,2.6% xsd00722,05/2007,SSG-PMD-Omegamon,1.9% xsd00722,05/2007,SSG-RAC-Maestro ,23.1%

        In addition, after the last Server entry, I need to output a TOTAL line with the Total Percentage for all Applications for a particular Server and Date. For the server/date combination (xsd00501,05/2007) above, a line after the last line with xsd00501,05/2007, would be printed totalling the percentages, as follows:

        xsd00501,05/2007,TOTAL,51.6%

        Up to this point in my limited perl programming experiences,I have been unable to grasp how to determine when a particular variable changes its value in the input and when this happens doing a particular task.

        Any assistance would be greatly appreciated.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (3)
As of 2014-07-13 03:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls