in reply to Re^2: hoh or hoa data structure
in thread hoh or hoa data structure
Something like:
#!/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;
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
You'll have to fix the 'total' calculation to get the results you show however. My sample code simply sums the values.
|
|---|