in reply to Data Set Combination
#!/usr/bin/perl -w use strict; use DBI; my $pair1 = shift; my $pair2 = shift; my $dbh = DBI->connect("DBI:CSV:") or die "Cannot connect: " . $DBI::errstr; $dbh->{RaiseError} = 1; my $query1 = "SELECT * FROM $pair1"; my $query2 = "SELECT * FROM $pair2"; my $getpair1 = $dbh->prepare($query1); my $getpair2 = $dbh->prepare($query2); my %pair1_hash = do{ $getpair1->execute(); while($_ = $getpair1->fetchrow_hashref()){ $_{$_->{Date}} = $_; } %_; }; my %pair2_hash = do{ $getpair2->execute(); while($_ = $getpair2->fetchrow_hashref()){ $_{$_->{Date}} = $_; } %_; }; my %combo_hash = do{ map{ if(!defined $pair1_hash{$_}){ $_, $pair2_hash{$_}; }elsif(!defined $pair2_hash{$_}){ $_, $pair1_hash{$_}; }else{ #computations here to combine the contents of #%pair1_hash and %pair2_hash. Utilize the fact that #the last statement evaluated will be the return value. #just as and example my %t; #reference to specific fields within each hash referred to #by $pair1_hash{$_} and $pair2_hash respectively $t{Open} = sprintf("%.2f",($pair1_hash{$_}->{Open} + $pair +2_hash{$_}->{Open} / 2)); $t{High} = sprintf("%.2f",($pair1_hash{$_}->{High} + $pair +2_hash{$_}->{High} / 2)); #So finally we return the key and a reference to #our computed hash. $_,\%t; } }do{ undef @_{keys %pair1_hash, keys %pair2_hash}; sort keys %_; }; };
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Data Set Combination
by Anonymous Monk on Aug 29, 2005 at 14:23 UTC |