rocky13 has asked for the wisdom of the Perl Monks concerning the following question:
Below you will find the entire code. The code does calculations on two of the columns once they are ordered based on first three columns. Instead of "printing", how do I use "Format" to write a report? I am trying to get the header to say "Totals", column names to say (Name, Letter, Number, Grade1, Grade2). There should be a solid break like after the columns names and a dashed line after each "totals line". I need to write the report to an output file. My main problem was that I was not able to get the results printed in the report. I have never used format before so I have no clue. Hopefully, you won't have to change the code. References for further learning would also help. Thanks in advance.
#!/usr/bin/perl use strict; use warnings; use DBI; my $tableName = 'hist'; unlink $tableName; open my $resFile, '>', $tableName; print $resFile <<DATA; e_id,cus,ta,gd1,gd2 Joe,A,1,85,90 Joe,A,1,80,99 Joe,A,2,50,70 Joe,A,2,60,65 Joe,A,2,87,89 Joe,B,1,82,92 Joe,B,3,30,51 Rob,A,1,64,77 Rob,B,2,20,32 DATA close $resFile; my $dbh = DBI->connect("DBI:CSV:") or die $DBI::errstr; my $sth = $dbh->prepare("SELECT * FROM $tableName"); $sth->execute(); my $query = $dbh->selectall_arrayref( "select e_id, cus, ta, gd1, gd2 from $tableName order by e_id, cus +, ta", {Slice => {}}); my $gd1Total; my $gd2Total; my $currGroup; for my $row (@$query, {map {$_, 0} qw(e_id cus ta gd1 gd2)}) { my $group = join ',', @{$row}{qw(e_id cus ta)}; $currGroup = $group if !defined $currGroup; if ($group ne $currGroup) { print " Total $gd1Total, $gd2Total\n"; $gd1Total = $gd2Total = 0; $currGroup = $group; last if $group eq '0,0,0'; } print +(join ', ', @{$row}{qw(e_id cus ta gd1 gd2)}), "\n"; $gd1Total += $row->{gd1}; $gd2Total += $row->{gd2}; } exit; [download] Prints: Joe, A, 1, 85, 90 Joe, A, 1, 80, 99 Total 165, 189 Joe, A, 2, 50, 70 Joe, A, 2, 60, 65 Joe, A, 2, 87, 89 Total 197, 224 Joe, B, 1, 82, 92 Total 82, 92 Joe, B, 3, 30, 51 Total 30, 51 Rob, A, 1, 64, 77 Total 64, 77 Rob, B, 2, 20, 32 Total 20, 32
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: writing report using format
by CountZero (Bishop) on Jan 18, 2011 at 08:34 UTC | |
Re: writing report using format
by eff_i_g (Curate) on Jan 18, 2011 at 04:52 UTC | |
by rocky13 (Acolyte) on Jan 18, 2011 at 10:19 UTC | |
by stefbv (Curate) on Jan 18, 2011 at 11:41 UTC |