rocky13 has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I have queried results from a database as hash reference. Now, I wish to put these results in a data structure. The results are in order based on the first three columns. I am trying to get the following structure:
Basically, I need to do calculations on the last two columns: Joe,A,1's would get calculated, Joe,A,2's would get calculated,Joe,B,1's would get calculated etc. Please let me know if there is a better approach. I was having trouble separating the rows Joe,A,1's from Joe,A,2's and therefore i was not able to do any calculations. If there is a way just to separate the rows based on the first three columns, then I know how to do the rest. Thanks in advance! Here is the code with the subset of data I queried:Joe=>{ A=>{ 1=> { [85,80],[90,99] } 2=> { [50,60,87],[70,65,89] } } B=>{ 1=> { [82],[92] } 3=> { [30],[51] } } } Rob => continue as above
#!/usr/bin/perl use DBI; use DBD::mysql; print "Connecting...\n"; 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 e_id,cus,ta,gd1,gd2 from +hist order by e_id,cus,ta", {Slice => {} }); foreach my $ref (@$query) { print "$ref->{e_id},$ref->{cus},$ref->{ta},$ref->{gd1},$ref->{gd2}\ +n"; } <p> Here is the subset of data I get from this code. </p> 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
At the end, I want to be able to calculate the 4th and 5th column like this:
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 147,154 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: hoh or hoa data structure
by Generoso (Prior) on Jan 11, 2011 at 00:08 UTC | |
by rocky13 (Acolyte) on Jan 11, 2011 at 00:34 UTC | |
by planetscape (Chancellor) on Jan 11, 2011 at 01:46 UTC | |
|
Re: hoh or hoa data structure
by GrandFather (Saint) on Jan 10, 2011 at 23:25 UTC | |
by rocky13 (Acolyte) on Jan 10, 2011 at 23:39 UTC | |
by GrandFather (Saint) on Jan 11, 2011 at 00:46 UTC | |
|
Re: hoh or hoa data structure
by eff_i_g (Curate) on Jan 11, 2011 at 00:29 UTC | |
|
Re: hoh or hoa data structure
by ahmad (Hermit) on Jan 11, 2011 at 00:38 UTC | |
by Generoso (Prior) on Jan 11, 2011 at 00:41 UTC | |
by rocky13 (Acolyte) on Jan 18, 2011 at 03:56 UTC |