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

In reply to array of arrays calculations by rocky13

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.