vito pascali wrote:
Ok ppl,
after some hard work (for me at least!!) that's what I got:
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use DBD::mysql;
use warnings;
my (
$db_gal,$db_gal2,$db_lab,$tbl_ary_ref_g1,$tbl_ary_ref_g2,$tbl_ary_ref_l1 );
You should declare your variables in the smallest scope possible, not
all at the beginning of your program.
###########################################################################
#Query G1 from the db test_gal, the results are: [alfa , 10] , [beta , 11];
###########################################################################
$db_gal = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" )
|| die( $DBI::errstr . "\n" );
The '||' operator has higher precedence than the '=' operator so you
should use parentheses around the '=' expression:
( my $db_gal = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" ) )
|| die $DBI::errstr . "\n";
Or use the lower precedence 'or' operator:
my $db_gal = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" )
or die $DBI::errstr . "\n";
my $SEL_G1 = "select col1,col2 from GAL";
my $query_handle_gal = $db_gal->prepare($SEL_G1);
$query_handle_gal->execute();
$tbl_ary_ref_g1 = $query_handle_gal->fetchall_arrayref();
my $tbl_ary_ref_g1 = $query_handle_gal->fetchall_arrayref();
You should put this code in a block and populate %hash (bad name for a
variable BTW) here instead of populating three arrays and undefining
them later.
############################################################################
#Query L1 from the db test_lab, the results are: [gamma , 12] , [alfa , 10];
############################################################################
$db_lab = DBI->connect(
"dbi:mysql:test_lab:localhost:3306","user","password" )
|| die( $DBI::errstr . "\n" );
my $SEL_L1 = "select * from LAB";
my $query_handle_lab = $db_lab->prepare($SEL_L1);
$query_handle_lab->execute();
$tbl_ary_ref_l1 = $query_handle_lab->fetchall_arrayref();
###########################################################################
#Query G2 from the db test_gal, the results are:(gamma);
###########################################################################
$db_gal2 = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" )
|| die( $DBI::errstr . "\n" );
my $SEL_G2 = "select * from GAL2";
my $query_handle_gal2 = $db_gal2->prepare($SEL_G2);
$query_handle_gal2->execute();
$tbl_ary_ref_g2 = $query_handle_gal2->fetchall_arrayref();
#########################################
my @unique;
my @overlap;
my %hash;
foreach my $result_gal (@$tbl_ary_ref_g1) {
my $key = join '*|*', @{$result_gal};
$hash{ $key }++;
}
undef (@$tbl_ary_ref_g1);
foreach my $result_lab (@$tbl_ary_ref_l1) {
my $key = join "*|*", @{$result_lab};
$hash{ $key }++;
}
undef (@$tbl_ary_ref_l1);
foreach my $result_gal2 (@$tbl_ary_ref_g2) {
my $key = join "*|*", @{$result_gal2};
$hash{ $key }++;
}
undef (@$tbl_ary_ref_g2);
foreach my $key ( keys %hash ) {
if ( $hash{ $key } == 1 ) {
my @array = split ( /\*\|\*/, $key );
push @unique, [@array];
@array (again, bad name for a variable) is local in scope to the if
block so there is no need to copy it, just pass a reference to it:
my @array = split /\*\|\*/, $key;
push @unique, \@array;
Or do it without the array:
push @unique, [ split /\*\|\*/, $key ];
} else {
my @array = split /\*\|\*/, $key;
push @overlap, [@array];
}
}
John
--
Any intelligent fool can make things bigger and
more complex... It takes a touch of genius -
and a lot of courage to move in the opposite
direction. -- Albert Einstein
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/