I'm working with 100's of PS Account 'MONTHLY USAGE'
Reports. I have been able to parse the reports, do a
table lookup to match a Login ID with an Application
Name. My CSV up to this point is as follows:
Server,Date,Login ID,Application,CPU Minutes,Percent of Total xsd00544,05/2007,s2pe,CTI Production ,8455,21.7% xsd00544,05/2007,sybprd0l,OMS,5326,13.7% xsd00544,05/2007,pctip01,CTI,5098,13.1% xsd00544,05/2007,pitip01,CTI,1742,4.5% xsd00544,05/2007,pipmp01,SSG-IPM,1742,4.5% xsd00544,05/2007,maestro,SSG-RAC-Maestro ,1020,2.6% xsd00544,05/2007,systro,SSG-RAC-Maestro ,836,2.1% xsd00544,05/2007,f2pa,"DB2DARI ""stored procedures"" prod",836,2.1% xsd00544,05/2007,pomgp01,SSG-PMD-Omegamon,56,0.1% xsd00544,05/2007,pptip01,PTI - Private Client Services,16,0.0% xsd00544,05/2007,pbmwp01,BMW,3,0.0% xsd00544,05/2007,s2pv,Merva,1,0.0%

I am trying to accumulate the Percent of Totals by
Application. A real example will have multiple Login
ID's (the 3rd field) per Application (the 4th field).
The code I am currently testing is as follows:
#!/usr/bin/perl use strict; use warnings; my $i = 0; my $mcpupct=0; my (%total, $total); for my $file ("PSAMARTst1.csv") { open (my $SORTED,"<",$file) or die "Can't open file $file: $!"; open (my $OUTCSV,">","PSAMARTst1out.csv") or die "Can't open OUT fil +e: $!"; while (my $line = <$SORTED>) { chomp($line); $line =~ s/%//g; ## Remove % Signs so that Percentages can be Op +erated On next if $line =~ /^Server,Date/; my ($server,$date,$login,$appl,$cpumin,$cpupct) = (split(",",$line)) +; ## if ($. == 0) { ## print $OUTCSV "$server $date, \n"; ## print $OUTCSV " \n"; ## } $mcpupct += $cpupct; $total{$appl} += $cpupct; } foreach my $appl (sort keys %total) { printf $OUTCSV ("$appl,%3.1f%% \n", $total{$appl}); ## printf $OUTCSV ("$server,$date,$appl,%3.1f%% \n", $total{$appl}); } if (eof($SORTED)) { printf $OUTCSV ("TOTAL,%3.1f%% \n", $mcpupct); ## printf $OUTCSV ("$server,$date,TOTAL,%3.1f%% \n", $mcpupct); } close $SORTED or die "Can't close input SORTED file: $!"; close $OUTCSV or die "Can't close PSAMARTst1out.csv data file: $!"; } ########################################################## # # Sort PSAMARTst1out.csv # Descending by CPU Percentage # ########################################################## system("sort -t, -n -r -o PSASortTst2.dat +1 PSAMARTst1out.csv");

The problem I am having is that I need to retain the
$server and $date variables in the output csv created
prior to the system call to the unix sort command.


Since I am accumulating these totals in a foreach loop
using an array outside of the while loop (where the
$server and $date variables are assigned), there is
no way for me to output these variables with the rest
of the output record.


I am relatively new to perl (SAS is my weapon of
choice)- can these totals be accumulated in another
manner, so that I will be able to retain these 2
variables in my output? I have also read various
documentation concerning global variables and the our
statement in attempt to somehow retain these 2
variables. I have had no luck in determining how to
solve this problem up to this point - any assistance
would be kindly appreciated.


In reply to Accumulating Column Total From a CSV for a Common Key Value by GeneV1

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.