rocky13 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have queried two similar result sets from a database as hash reference. For matching rows, I need to subtract set1 - set 2 for columns 3 and 4. Thanks! Here is the code with the subset of data I queried:
#!/usr/bin/perl use DBI; use DBD::mysql; my $platform = "mysql"; my $database = "db_name"; my $host = "host_name"; my $port = "port_num"; my $tablename = "hist"; my $user = "user_name"; my $pwd = "pass"; open(STDOUT, ">C:\\perlscripts\\new.txt") || die "Can't open the file" +; my $dsn = "dbi:mysql:$database:$host:$port"; my $dbh = DBI->connect($dsn,$user,$pwd) || die "Could not connect: $DB +I::errstr\n"; my $query = $dbh->selectall_arrayref("select cus,ta,sum(gd1) as gd1,su +m(gd2) as gd2 from hist group by cus,ta order by cus", {Slice => {} } +); my $query2 = $dbh->selectall_arrayref("select cus,ta,sum(gd1),sum(gd2) + from current group by cus,ta order by cus", {Slice => {} }); foreach my $ref (@$query) { push(@{$arry}, join(",",$ref->{cus},$ref->{ta},$ref->{gd1},$ref->{g +d2}))."\n"; } foreach my $ref (@$query2) { push(@{$arry2}, join(",",$ref->{cus},$ref->{ta},$ref->{gd1},$ref->{ +gd2}))."\n";
Here is the subset of data I get from this code. For columns 3 and 4, I am basically subtracting set1 - set2 (if match is found in set2, otherwise just print set1). I don't know what approach will allow me to get to the results. What would be the right approach?? Thanks.
set 1: set 2: Joe,A,85,90 Joe,A,80,85 Joe,B,80,99 Joe,B,70,90 Rob,A,50,70 Rob,A,40,70 Rob,B,60,65 Tim,A,70,89 Tim,A,87,89 Lou,A,30,51 Jon,B,82,92 Lou,A,30,51 Output: Joe,A,5,5 Joe,B,10,9 Rob,A,10,0 Rob,B,60,65 Tim,A,17,0 Jon,B,82,92 Lou,A,0,0
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: array of arrays calculations
by thezip (Vicar) on Jan 19, 2011 at 04:41 UTC | |
by CountZero (Bishop) on Jan 19, 2011 at 07:07 UTC | |
by Generoso (Prior) on Jan 19, 2011 at 17:12 UTC | |
by rocky13 (Acolyte) on Jan 20, 2011 at 02:28 UTC | |
Re: array of arrays calculations
by JavaFan (Canon) on Jan 19, 2011 at 10:12 UTC |