
multidimensional array check
--001517503b266cbb6a049d97314d
Content-Type: text/plain; charset=ISO-8859-1
Hi there,
I'm really new to perl and programmation too so don't bear on me if I will
say something wrong pls... :)
The problem:
I have 3 querys to 2 different oracle databases:
1)script query G1:
#!/usr/bin/perl -w
use DBI;
use warnings;
my ( $db );
$db = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=XX", "x", "x" )
|| die( $DBI::errstr . "\n" );
my $SEL_G1 = "select x,y from table";
my $query_handle = $db->prepare($SEL_G1);
$query_handle->execute();
$query_handle->bind_columns(undef,\$x, \$y);
while (my [at] result_g1 = $query_handle->fetchrow_array){
print "$x-$y\n";
}
$db->disconnect if defined($db);
This print:
1 [at] 1600 [at] 1 [at] 1600A [at] 1 [at] alfa [at] 1-10125691-20110301102500
-------------------------------------------------
2)script Query G2:
#!/usr/bin/perl -w
use DBI;
use warnings;
my ( $db2);
$db2 = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=XX", "x", "x" )
|| die( $DBI::errstr . "\n" );
my $SEL_G2 = "select x from table";
my $query_handle_2 = $db2->prepare($SEL_G2);
$query_handle_2->execute();
$query_handle_2->bind_columns(undef,\$x);
while (my [at] result_gal = $query_handle_2->fetchrow_array){
print "$x\n";
}
$db2->disconnect if defined($db2);
This print:
1 [at] 1501 [at] 1 [at] 1501CPL1 [at] 1 [at] alfa
----------------------------------------------------------
3) Query L1:
#!/usr/bin/perl -w
use DBI;
use warnings;
my ( $db1 );
$db1 = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=YY", "y",
"y")
|| die( $DBI::errstr . "\n" );
my $SEL_L1 = "select x,y from table"
my $query_handle_1 = $db1->prepare($SEL_L1);
$query_handle_1->execute();
$query_handle_1->bind_columns(undef,\$x, \$y);
while($query_handle_1->fetch()) {
print "$x-$y";
print "\n";
}
$db1->disconnect if defined($db1);
This print:
1 [at] 1206 [at] 1 [at] 1206 [at] 1 [at] alfa [at] 1-10123745-20110303162900
I need that the results from the query L1 is checked against the results
from the query G1 and that what differs between will be checked finally
against the results of G2.
The problems are:
1)the results are huge, specially what come from G1;
2)2 of the 3 arrays ( [at] G1 e [at] L1) and one not ( [at] G2);
3)I'm really a newbie....
So what would be for you the best (and the fastest) way to do the job?
Example,link and RTFM are really appreciate!
Tnx in advance
Vito
--001517503b266cbb6a049d97314d--
Re: multidimensional array check
On 3/3/11 Thu Mar 3, 2011 9:16 AM, "vito pascali" <vito.pascali [at] gmail.com>
scribbled:
> Hi there,
> I'm really new to perl and programmation too so don't bear on me if I will
> say something wrong pls... :)
>
> The problem:
>
> I have 3 querys to 2 different oracle databases:
[queries snipped]
> I need that the results from the query L1 is checked against the results
> from the query G1 and that what differs between will be checked finally
> against the results of G2.
The most efficient way would be to arrange to have the Oracle database
engine do most of the comparisons. I am not enough of a database expert to
recommend ways to do this.
The fastest way to do this in Perl would be to save the results of one query
in memory in a data structure of some type, either an array or a hash. Then,
as the results from the second query are fetched, compare against the copy
in memory and save what differs (you have not explained how to decide when
the two results differ). This will be possible only if the results from one
query will fit into available memory. To maximize the probability of that
happening, save the query with the least amount of data.
> The problems are:
>
> 1)the results are huge, specially what come from G1;
How huge? Some representative numbers would help.
> 2)2 of the 3 arrays ( [at] G1 e [at] L1) and one not ( [at] G2);
I do not understand what you are saying. Are you saying that queries G1 and
L1 are huge and G2 is not?
> 3)I'm really a newbie....
We were all newbies at one time.
>
> So what would be for you the best (and the fastest) way to do the job?
> Example,link and RTFM are really appreciate!
This is a rather specific problem for which you are unlikely to find a
pre-packaged solution, but you might get lucky.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
On 03/03/2011 17:16, vito pascali wrote:
>
> I'm really new to perl and programmation too so don't bear on me if I will
> say something wrong pls... :)
>
> The problem:
>
> I have 3 querys to 2 different oracle databases:
>
>
> 1)script query G1:
>
> #!/usr/bin/perl -w
> use DBI;
> use warnings;
> my ( $db );
>
> $db = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=XX", "x", "x" )
> || die( $DBI::errstr . "\n" );
>
> my $SEL_G1 = "select x,y from table";
>
> my $query_handle = $db->prepare($SEL_G1);
>
> $query_handle->execute();
> $query_handle->bind_columns(undef,\$x, \$y);
> while (my [at] result_g1 = $query_handle->fetchrow_array){
> print "$x-$y\n";
> }
> $db->disconnect if defined($db);
>
> This print:
> 1 [at] 1600 [at] 1 [at] 1600A [at] 1 [at] alfa [at] 1-10125691-20110301102500
>
>
> -------------------------------------------------
>
> 2)script Query G2:
>
> #!/usr/bin/perl -w
>
> use DBI;
> use warnings;
> my ( $db2);
> $db2 = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=XX", "x", "x" )
> || die( $DBI::errstr . "\n" );
>
> my $SEL_G2 = "select x from table";
>
> my $query_handle_2 = $db2->prepare($SEL_G2);
>
> $query_handle_2->execute();
> $query_handle_2->bind_columns(undef,\$x);
> while (my [at] result_gal = $query_handle_2->fetchrow_array){
> print "$x\n";
> }
> $db2->disconnect if defined($db2);
>
> This print:
> 1 [at] 1501 [at] 1 [at] 1501CPL1 [at] 1 [at] alfa
>
>
> ----------------------------------------------------------
>
> 3) Query L1:
>
> #!/usr/bin/perl -w
>
> use DBI;
> use warnings;
> my ( $db1 );
>
> $db1 = DBI->connect( "dbi:Oracle:host=x.x.x.x;sid=YY", "y",
> "y")
> || die( $DBI::errstr . "\n" );
>
> my $SEL_L1 = "select x,y from table"
>
> my $query_handle_1 = $db1->prepare($SEL_L1);
>
> $query_handle_1->execute();
>
> $query_handle_1->bind_columns(undef,\$x, \$y);
> while($query_handle_1->fetch()) {
> print "$x-$y";
> print "\n";
> }
> $db1->disconnect if defined($db1);
>
> This print:
> 1 [at] 1206 [at] 1 [at] 1206 [at] 1 [at] alfa [at] 1-10123745-20110303162900
>
> I need that the results from the query L1 is checked against the results
> from the query G1 and that what differs between will be checked finally
> against the results of G2.
> The problems are:
>
> 1)the results are huge, specially what come from G1;
> 2)2 of the 3 arrays ( [at] G1 e [at] L1) and one not ( [at] G2);
> 3)I'm really a newbie....
>
> So what would be for you the best (and the fastest) way to do the job?
> Example,link and RTFM are really appreciate!
You haven't told us enough to understand the problem. You are performing
'select x,y from table' and want to "check it against" the results of
'select x from table'. I can assure you that it will be the same but
with an extra column in the first set.
Are 'x', 'y', and 'table' different in each case perhaps? If so then you
must explain in detail what you mean by 'check against'. It is likely
that any comparison between the two results from the same database can
be performed in SQL. Depending on how you have Oracle set up, you may be
able to do the same with the third set.
What is the purpose of the initial 'undef' in your calls to
bind_columns? If your code is genuinely as you have shown it, then the
value of x will be discarded. Also, the output you show doesn't look
like it came from the print statements you are using.
Could you explain better what it is you are trying to achieve please?
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--------------090603030301080004090004
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
First of all, thank for u support! Really !
> The most efficient way would be to arrange to have the Oracle database
> engine do most of the comparisons. I am not enough of a database expert to
> recommend ways to do this.
I agree with u , but it's something that is not possible without a
dblink, that's why I'm here to write some code.... :)
> The fastest way to do this in Perl would be to save the results of one query
> in memory in a data structure of some type, either an array or a hash. Then,
> as the results from the second query are fetched, compare against the copy
> in memory and save what differs (you have not explained how to decide when
> the two results differ).
That's exactly what I was trying to do,and u are right probably I was
not so clear about "how to decide when the two results differ".
Let's try to put some light on:
The results of the first query will be an array something like this:
[at] G1 = (["alfa" , "10"] ,
["beta" , "11"]);
Similar the second query will give this kind of array:
[at] L1 = (["alfa" , "10"],
["gamma" , "12"]);
And finally the third query:
[at] G2 = ("gamma");
G1 and G2 are two query on the same db and same schema too.
L1 is on another db server and of course on a different schema.
So what I need is to discharge the results that I found in all the three
queries and take only what differ.
For something coming directly from the data nature I need to check
before the G1 and L1 and then check what differ on G2.
In example the results that i need is the couple "beta 11".
>
> How huge? Some representative numbers would help.
>
The results of the query of G1 is on the order of 100k+ records, and the
machine that will do the perl code is a linux dell with 3GB of memory
and a cpu AMD Opteron 1.7GHz,..
> I do not understand what you are saying. Are you saying that queries G1 and
> L1 are huge and G2 is not?
No I was just trying to say that i had problem to check difference
against the G2 query couse it just have one column of results (normal
array) where the results coming from the other two query are a 2 column
(multidimensional array).
>> We were all newbies at one time.
Hope to grow up then :)
Again tnx all for u help!
--
Vito Pascali
ICT Security Manager
IT Senior System Administrator
vito.pascali [at] gmail.com
--------------090603030301080004090004--
Re: multidimensional array check
> You haven't told us enough to understand the problem.
You are right as i said before i wasn't really clear..
>
> Are 'x', 'y', and 'table' different in each case perhaps?
Yes
> If so then you must explain in detail what you mean by 'check
> against'. It is likely
> that any comparison between the two results from the same database can
> be performed in SQL. Depending on how you have Oracle set up, you may be
> able to do the same with the third set.
That's not possible because the tables are not even on the same
database server and we don't have the grants to make a dblink... so here
I'm :)
>
> What is the purpose of the initial 'undef' in your calls to
> bind_columns? If your code is genuinely as you have shown it, then the
> value of x will be discarded. Also, the output you show doesn't look
> like it came from the print statements you are using.
>
I can say for sure that the only code that i commented out are the
oracle stuff (select and so on..) and the ip and the password of the
servers.
The rest of the code is really like i had write here.
> Could you explain better what it is you are trying to achieve please?
>
I hope that the other mail i already sent will give more hints about my
problem!
Anyway tnx u too for u help :)
--
Vito Pascali
ICT Security Manager
IT Senior System Administrator
vito.pascali [at] gmail.com
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--20cf3071d0daeb2171049da74067
Content-Type: text/plain; charset=UTF-8
Hi Vito,
Ok so lets give this a try...
First lets deal with comparing the arrays then we can get to the size issues
and memory usage etc...
You have the data in an array which is good but not all that nice as after
all it will make for a lot of work if you want to compare two arrays (you
end up having to compare all elements in array 1 with all elements in array
2 which means with 100 elements in each array already 100x100 = 10000
comparisons which if your data sets grow will only get worse.
So lets use a hash instead as this allows for a key value and used in a
intelligent way will save you a lot of comparing. Here is what I would do:
use strict;
use warnings;
my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
my [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
my [at] unique;
my [at] overlap;
my %hash;
foreach my $result ( [at] G1 ) {
my $key = join '*|*', [at] {$result}; # Makes ["alfa", "10"] into "alfa*|*10"
# [at] {$result} tells perl that $result contains an array
otherwise this whole trick would not work ;)
$hash{ $key }++;
}
# We now see in %hash the following key value pairs "alfa*|*10" => 1 and
"beta*|*11" => 1
# all that is left is adding to the the other array and we will find the
over lap automatically
# Since we will never use [at] G1 again we can drop this
undef [at] G1; # Basically sets [at] G1 to undef and lets perl know we will never
use this data again so it is free to clean up the memory
foreach my $result ( [at] L1 ) {
my $key = join "*|*", [at] {$result}; #makes ["alfa", "10"] into "alfa*|*10"
# [at] {$result} tells perl that $result contains an array
otherwise this whole trick would not work ;)
$hash{ $key }++;
}
# %hash will now contain "alfa*|*10" => 2 and "beta*|*11" => 1
# finding out which once are unique is easy we simply remove all that have a
value greater then 1
# Since we will never use [at] L1 again we can drop this
undef [at] L1; # Basically sets [at] L1 to undef and lets perl know we will never
use this data again so it is free to clean up the memory
foreach my $key ( keys %hash ) {
if ( $hash{ $key } == 1 ) { # This key only appeared once
my [at] array = split ( /\*\|\*/, $key ); # Pull the key apart into the two
values (or 200k values if you have that many columns o course)
# Now we add this to the [at] unique array
push [at] unique, [ [at] array];
} else { # This is where we deal with the over lapping results
my [at] array = split /\*\|\*/, $key; # Pull the key apart into the two values
(or 200k values if you have that many columns o course)
# Now we add this to the [at] overlap array
push [at] overlap, [ [at] array];
}
}
use Data::Dumper;
print "Overlap\n";
print Dumper [at] overlap;
print "Unique\n";
print Dumper [at] unique;
Thats easy enough right? Of course the perl purists will say use map, etc
but that would hide a bit to much about the logic of what is going on from a
beginner of course you should know that this ca all be done a lot faster and
more efficient etc. But for a beginner it is more important that you
understand the logic of it all then that you know the fancy tricks without
understanding how they work.
Anyway I think this is what you are looking for... A DB link is always a
hard thing to get approved I know that one besides depending on the location
of the databases you might not gain all that much by having the database
deal with this for you due to network latency and such things. If you did
want to do this on the DB level look at the oracle MINUS command for the
easiest way to implement this though there are others of course ;-)
I hope this helps you a bit, regards,
Rob
On Fri, Mar 4, 2011 at 12:30 PM, mammoccio <vito.pascali [at] gmail.com> wrote:
> First of all, thank for u support! Really !
>
> > The most efficient way would be to arrange to have the Oracle database
> > engine do most of the comparisons. I am not enough of a database expert
> to
> > recommend ways to do this.
> I agree with u , but it's something that is not possible without a
> dblink, that's why I'm here to write some code.... :)
> > The fastest way to do this in Perl would be to save the results of one
> query
> > in memory in a data structure of some type, either an array or a hash.
> Then,
> > as the results from the second query are fetched, compare against the
> copy
> > in memory and save what differs (you have not explained how to decide
> when
> > the two results differ).
> That's exactly what I was trying to do,and u are right probably I was
> not so clear about "how to decide when the two results differ".
> Let's try to put some light on:
>
> The results of the first query will be an array something like this:
>
> [at] G1 = (["alfa" , "10"] ,
> ["beta" , "11"]);
>
> Similar the second query will give this kind of array:
>
> [at] L1 = (["alfa" , "10"],
> ["gamma" , "12"]);
>
> And finally the third query:
>
> [at] G2 = ("gamma");
>
>
> G1 and G2 are two query on the same db and same schema too.
> L1 is on another db server and of course on a different schema.
>
> So what I need is to discharge the results that I found in all the three
> queries and take only what differ.
> For something coming directly from the data nature I need to check
> before the G1 and L1 and then check what differ on G2.
> In example the results that i need is the couple "beta 11".
>
> >
> > How huge? Some representative numbers would help.
> >
> The results of the query of G1 is on the order of 100k+ records, and the
> machine that will do the perl code is a linux dell with 3GB of memory
> and a cpu AMD Opteron 1.7GHz,..
>
> > I do not understand what you are saying. Are you saying that queries G1
> and
> > L1 are huge and G2 is not?
> No I was just trying to say that i had problem to check difference
> against the G2 query couse it just have one column of results (normal
> array) where the results coming from the other two query are a 2 column
> (multidimensional array).
>
> >> We were all newbies at one time.
>
> Hope to grow up then :)
>
> Again tnx all for u help!
>
>
> --
> Vito Pascali
> ICT Security Manager
> IT Senior System Administrator
> vito.pascali [at] gmail.com
>
>
--20cf3071d0daeb2171049da74067--
Re: multidimensional array check
--------------010005000309010600050501
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Il 04/03/2011 13:26, Rob Coops ha scritto:
First of all, really tnx Rob! I really appreciate u way to teach (even
to folks with no programming background like me) the logic behind!
I know that maybe this is a silly question but how can I transform the
my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
In something that take data from the oracle select statement ?
I mean I tried something like:
my [at] G1 = $query_handle->fetchrow_array
But it simply don't works....
I was at this point before and I got no solution because I always see
example that using something like
while (my [at] G1 = $query_handle->fetchrow_array)
{ do something
};
and I didn't found a simply solution to put stuff in a array...
Sorry to bother with so silly question, but really I want to learn how
to do it!
--
Vito Pascali
ICT Security Manager
IT Senior System Administrator
vito.pascali [at] gmail.com
--------------010005000309010600050501--
Re: multidimensional array check
--20cf3071c9123a7ae4049daafec1
Content-Type: text/plain; charset=UTF-8
You are now talking about the finer points of the DBI module... :-)
The simplest way of doing what you want is this:
my $tbl_ary_ref = $query_handle->fetchall_arrayref;
Here though you will need a bit of background as this does exactly what you
are looking for but with a twist. I'll try to explain...
In perl every value you store no mater if it is a scalar ($x = 1) an array
( [at] x = [0]) or an hash (%x = { 'key' => 1}) they all are stored in your
computers memory. And when you say my $y = $x; what perl will do is make a
copy of $x and call that $y. Now you are taking twice as much memory which
is not a problem if $x has a value of '1' but if $x contains the collected
works of 19th century poets then this can be quite expensive in terms of
memory usage.
So there is another way to achieve the same instead of saying my $y = $x;
you can say my $y = \$x; what perl does in this case is fill $y with a
reference to $x. This reference tells perl where in the memory $x is stored.
this way you save a lot of memory and still have two variables with
identical values in them. Now I suspect that you are already thinking wait a
moment if $y only points to $x and I change something in $x will this not
influence $y, and you are right. For what we are going to be doing this is
not that important but it is good to know for trouble shooting "strange"
behavior in your future scripts. ;-)
Of course what you can do with scalars you can do with arrays and hash's as
well just as easy.
So what $tbl_ary_ref will contain results as a reference to array which
contains references to arrays for each row so basically [ ["column 1",
"column 2"], ["column 1", "column 2"]]. The only thing is you will need to
access this data...
Remember the comment I made about the [at] {$result} comment in my previous
email thats how it is done, perl sees only the reference so what we did
there is we told perl that it should treat this $result as an array.
So in your case to get to the actual value of column 1 on row 5 of the
result set you would write something like this:
my $row_ref = ${ $tbl_ary_ref }[4]; # Get to the right row (number 4 as perl
arrays start counting at 0 not at 1)
my $column_value = ${ $row_ref }[0]; # Using ${}[0] notation as [at] {}[0] just
like [at] array[0] will result in a warning though it will still work for
compatibility reasons
or of course if you want to be fancy:
my $value ${ ${ $tbl_ary_ref }[4] }[0]; # This will provide the same result
as above but is far less readable and not all that desirable because of it
A last point that you will want to know when you start working with
references is that you can always dereference something (basically make a
copy of what ever the reference is pointing to. So you can for instance do
this:
my [at] array = [ 'an array' ];
my $array_ref = \ [at] array;
my [at] copy_array = [at] {$array_ref};
Doing this means that you can safely make changes to the [at] copy_array without
having to worry about making a mess of the array that $array_ref is pointing
to. It will how ever cause you to have that array in memory twice, so there
is a price to pay for that.
So you should now be able to retrieve the whole result set as a reference to
an array containing references to arrays. And I hope that
my ramblings explained a bit how you can use that reference to get to the
underlying values. If you combine all of that I think you'll be able to work
out the way to get your program to work the way you want it to.
By the way I would if I where you pick a slightly less complex task to learn
a programming language with next time, this one is though interesting
certainly not an easy one to use as a way to learn the language. :-)
Regards,
Rob
On Fri, Mar 4, 2011 at 3:25 PM, mammoccio <vito.pascali [at] gmail.com> wrote:
> Il 04/03/2011 13:26, Rob Coops ha scritto:
>
> First of all, really tnx Rob! I really appreciate u way to teach (even
> to folks with no programming background like me) the logic behind!
>
>
> I know that maybe this is a silly question but how can I transform the
>
> my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
>
> In something that take data from the oracle select statement ?
>
> I mean I tried something like:
>
> my [at] G1 = $query_handle->fetchrow_array
>
> But it simply don't works....
>
> I was at this point before and I got no solution because I always see
> example that using something like
>
> while (my [at] G1 = $query_handle->fetchrow_array)
> { do something
> };
>
> and I didn't found a simply solution to put stuff in a array...
>
> Sorry to bother with so silly question, but really I want to learn how
> to do it!
>
> --
> Vito Pascali
> ICT Security Manager
> IT Senior System Administrator
> vito.pascali [at] gmail.com
>
>
--20cf3071c9123a7ae4049daafec1--
Re: multidimensional array check
Rob Coops wrote:
>
> So you should now be able to retrieve the whole result set as a
> reference to an array containing references to arrays. And I hope
> that my ramblings explained a bit how you can use that reference to
> get to the underlying values. If you combine all of that I think
> you'll be able to work out the way to get your program to work the
> way you want it to.
>
FWIW, I found your ramblings quite helpful, since I am working to get
more comfortable with variables, arrays, and references, on my way to
learning objects.
Coming from a basic understanding of C, the reference variable concept
sounds a lot like pointers in C. But, my "Learning Perl" and
"Intermediate Perl" books do not use that terminology. Am I on a
correct path to understanding by making this analogy?
Thanks,
Brian
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--90e6ba4fc44e198b3a049dacb529
Content-Type: text/plain; charset=UTF-8
You are very right a really nice explanation of this you can find in the
"Advanced Perl Programming" book by Oreilly see the link below
http://oreilly.com/catalog/advperl/excerpt/ch01.html
If you already went trough the other two then this is definitely a good one
to read as it goes that extra step and provides you with a good insight of
the underlying logic.
On Fri, Mar 4, 2011 at 7:30 PM, Brian F. Yulga <byulga [at] langly.dyndns.org>wrote:
>
> Rob Coops wrote:
>
>
>> So you should now be able to retrieve the whole result set as a
>> reference to an array containing references to arrays. And I hope
>> that my ramblings explained a bit how you can use that reference to
>> get to the underlying values. If you combine all of that I think
>> you'll be able to work out the way to get your program to work the
>> way you want it to.
>>
>>
> FWIW, I found your ramblings quite helpful, since I am working to get more
> comfortable with variables, arrays, and references, on my way to learning
> objects.
>
> Coming from a basic understanding of C, the reference variable concept
> sounds a lot like pointers in C. But, my "Learning Perl" and "Intermediate
> Perl" books do not use that terminology. Am I on a correct path to
> understanding by making this analogy?
>
> Thanks,
> Brian
>
>
--90e6ba4fc44e198b3a049dacb529--
Re: multidimensional array check
On 3/4/11 Fri Mar 4, 2011 10:30 AM, "Brian F. Yulga"
<byulga [at] langly.dyndns.org> scribbled:
> Coming from a basic understanding of C, the reference variable concept
> sounds a lot like pointers in C. But, my "Learning Perl" and
> "Intermediate Perl" books do not use that terminology. Am I on a
> correct path to understanding by making this analogy?
Perl references are indeed similar to C pointers, in that a reference
variable contains the "location" of another variable and allows one to
indirectly access the value of that variable, but there are some
differences. For example, one cannot do arithmetic on Perl references, e.g.
incrementing a reference to iterate through an array.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
Thanks for the link. That's exactly the explanation I was looking for...
I progressed quickly through the first Oreilly book (Learning Perl) but
slowed down considerably in the 2nd (Intermediate Perl). I found the
chapter on Subroutine References (7) more difficult than earlier
material (my brain nearly exploded when reading the section "returning a
subroutine from a subroutine") so I decided to stop and go back to
basics until I achieve a better understanding. It didn't occur to me to
open up the Advanced Perl Programming book (what I intended to read as
my 3rd book)!
Rob Coops wrote:
> You are very right a really nice explanation of this you can find in
> the "Advanced Perl Programming" book by Oreilly see the link below
>
> http://oreilly.com/catalog/advperl/excerpt/ch01.html
>
> If you already went trough the other two then this is definitely a
> good one to read as it goes that extra step and provides you with a
> good insight of the underlying logic.
>
> On Fri, Mar 4, 2011 at 7:30 PM, Brian F. Yulga
> <byulga [at] langly.dyndns.org <mailto:byulga [at] langly.dyndns.org>> wrote:
>
>
> Rob Coops wrote:
>
>
> So you should now be able to retrieve the whole result set as a
> reference to an array containing references to arrays. And I hope
> that my ramblings explained a bit how you can use that reference to
> get to the underlying values. If you combine all of that I think
> you'll be able to work out the way to get your program to work the
> way you want it to.
>
>
> FWIW, I found your ramblings quite helpful, since I am working to get
> more comfortable with variables, arrays, and references, on my way to
> learning objects.
>
> Coming from a basic understanding of C, the reference variable
> concept sounds a lot like pointers in C. But, my "Learning Perl" and
> "Intermediate Perl" books do not use that terminology. Am I on a
> correct path to understanding by making this analogy?
>
> Thanks, Brian
>
>
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
On 11-03-04 02:30 PM, Brian F. Yulga wrote:
> my brain nearly exploded when reading the section "returning a
> subroutine from a subroutine"
Just wait until you get to the part where you can treat an anonymous
subroutine as an object. :D
For details, see `perldoc -f bless`.
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
Shawn H Corey wrote:
> On 11-03-04 02:30 PM, Brian F. Yulga wrote:
> > my brain nearly exploded when reading the section "returning a
> > subroutine from a subroutine"
>
> Just wait until you get to the part where you can treat an anonymous
> subroutine as an object. :D
>
> For details, see `perldoc -f bless`.
>
"bless REF
This function tells the thingy referenced by REF that it
is now
an object in the CLASSNAME package. ..."
'thingy' is a great technical term. ;-)
When I learned rudimentary C/C++ in school, I was taught pointers and
structs/classes initially as separate concepts, and later the two were
connected. It's becoming apparent to me that learning Perl objects
_requires_ a solid understanding of references first...
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
On 11-03-04 05:01 PM, Brian F. Yulga wrote:
> "bless REF
> This function tells the thingy referenced by REF that it
> is now
> an object in the CLASSNAME package. ..."
>
> 'thingy' is a great technical term. ;-)
Yes, Perl is the only language where you can bless your thingy. :)
--
Just my 0.00000002 million dollars worth,
Shawn
Confusion is the first step of understanding.
Programming is as much about organization and communication
as it is about coding.
The secret to great software: Fail early & often.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--001517577072e076b0049df56f66
Content-Type: text/plain; charset=ISO-8859-1
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,$tb l_ary_ref_l1 );
############################################################ ###############
#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" );
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();
############################################################ ################
#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 [at] unique;
my [at] overlap;
my %hash;
foreach my $result_gal ( [at] $tbl_ary_ref_g1) {
my $key = join '*|*', [at] {$result_gal};
$hash{ $key }++;
}
undef ( [at] $tbl_ary_ref_g1);
foreach my $result_lab ( [at] $tbl_ary_ref_l1) {
my $key = join "*|*", [at] {$result_lab};
$hash{ $key }++;
}
undef ( [at] $tbl_ary_ref_l1);
foreach my $result_gal2 ( [at] $tbl_ary_ref_g2) {
my $key = join "*|*", [at] {$result_gal2};
$hash{ $key }++;
}
undef ( [at] $tbl_ary_ref_g2);
foreach my $key ( keys %hash ) {
if ( $hash{ $key } == 1 ) {
my [at] array = split ( /\*\|\*/, $key );
push [at] unique, [ [at] array];
} else {
my [at] array = split /\*\|\*/, $key;
push [at] overlap, [ [at] array];
}
}
#########################
use Data::Dumper;
print "Overlap:\n";
print Dumper [at] overlap;
print "Unique:\n";
print Dumper [at] unique;
The results of this scripts are:
Overlap:
$VAR1 = [
'alfa',
'10'
];
Unique:
$VAR1 = [
'gamma'
];
$VAR2 = [
'gamma',
'12'
];
$VAR3 = [
'beta',
'11'
];
Ok this is quite good but still is not what I was looking for.
The results that I expect against the 3 query is only (beta 11), couse is
the only couple that is really unique...
Any clues?
Vito Pascali
--001517577072e076b0049df56f66--
Re: multidimensional array check
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,$tb l_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 [at] unique;
> my [at] overlap;
>
> my %hash;
> foreach my $result_gal ( [at] $tbl_ary_ref_g1) {
> my $key = join '*|*', [at] {$result_gal};
> $hash{ $key }++;
> }
>
> undef ( [at] $tbl_ary_ref_g1);
>
> foreach my $result_lab ( [at] $tbl_ary_ref_l1) {
> my $key = join "*|*", [at] {$result_lab};
> $hash{ $key }++;
> }
>
> undef ( [at] $tbl_ary_ref_l1);
>
> foreach my $result_gal2 ( [at] $tbl_ary_ref_g2) {
> my $key = join "*|*", [at] {$result_gal2};
> $hash{ $key }++;
> }
>
> undef ( [at] $tbl_ary_ref_g2);
>
> foreach my $key ( keys %hash ) {
> if ( $hash{ $key } == 1 ) {
> my [at] array = split ( /\*\|\*/, $key );
> push [at] unique, [ [at] array];
[at] 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 [at] array = split /\*\|\*/, $key;
push [at] unique, \ [at] array;
Or do it without the array:
push [at] unique, [ split /\*\|\*/, $key ];
> } else {
> my [at] array = split /\*\|\*/, $key;
> push [at] overlap, [ [at] 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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--0016e64988f671d20f049e0d31bd
Content-Type: text/plain; charset=ISO-8859-1
Tnx a lot for u tips!!
I just used all (or almost all) u suggestion, but what i really need is some
help in the "logic" of the script.
I mean at the moment I have this 3 arrays:
[at] G1 = (["alfa" , "10"], ["beta" , "11"]);
[at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
[at] G2 =('gamma');
I need to found only the unique couple of results: ["beta" , "11"] , couse
[gamma 12] and [gamma] have to be excluded.
What i obtain at the moment is instead:
Overlap:
$VAR1 = [
'alfa',
'10'
];
### Correct!! #########
Unique:
$VAR1 = [
'gamma' #### Not correct ########
];
$VAR2 = [
'gamma', #### Not correct ########
'12'
];
$VAR3 = [
'beta',### Correct!! #########
'11'
];
Any hints?
Vito
--0016e64988f671d20f049e0d31bd--
Re: multidimensional array check
At 3:03 PM +0100 3/9/11, vito pascali wrote:
>Tnx a lot for u tips!!
>I just used all (or almost all) u suggestion, but what i really need is some
>help in the "logic" of the script.
>I mean at the moment I have this 3 arrays:
>
> [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
> [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
> [at] G2 =('gamma');
>
>I need to found only the unique couple of results: ["beta" , "11"] , couse
>[gamma 12] and [gamma] have to be excluded.
#!/usr/bin/perl
use strict;
use warnings;
my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
my [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
my [at] G2 =('gamma');
# populate a hash with the elements of G1
my %unique;
for my $e1 ( [at] G1 ) {
$unique{$e1->[0]} = $e1->[1];
}
# add elements in L1 not in G1
# delete elements in both
my %overlap;
for my $e2 ( [at] L1 ) {
my( $key, $val ) = [at] $e2;
if( exists $unique{$key} ) {
$overlap{$key} = 1;
delete $unique{$key};
}else{
$unique{$key} = $val;
}
}
print "Overlap: ", join(', ',keys %overlap),"\n";
print "Unique: ", join(', ',keys %unique),"\n";
# eliminate unique elements also in G2
for my $e3 ( [at] G2 ) {
delete $unique{$e3};
}
print "Remains: ", join(", ", keys %unique), "\n";
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--20cf30050e2cc9ca3e049e0f7825
Content-Type: text/plain; charset=ISO-8859-1
Tnx Jim! I was looking at u script but still is not what I need:
>
>
>
> #!/usr/bin/perl
> use strict;
> use warnings;
>
> my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
> my [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
> my [at] G2 =('gamma');
>
> # populate a hash with the elements of G1
> my %unique;
> for my $e1 ( [at] G1 ) {
> $unique{$e1->[0]} = $e1->[1];
> }
>
> # add elements in L1 not in G1
> # delete elements in both
> my %overlap;
> for my $e2 ( [at] L1 ) {
> my( $key, $val ) = [at] $e2;
> if( exists $unique{$key} ) {
> $overlap{$key} = 1;
> delete $unique{$key};
> }else{
> $unique{$key} = $val;
> }
> }
>
> print "Overlap: ", join(', ',keys %overlap),"\n";
> print "Unique: ", join(', ',keys %unique),"\n";
>
> # eliminate unique elements also in G2
> for my $e3 ( [at] G2 ) {
> delete $unique{$e3};
> }
>
> print "Remains: ", join(", ", keys %unique), "\n";
The results of u script are:
Overlap: alfa
Unique: gamma, beta
Remains: beta
What I need is really different:
Overlap have to be: alfa,10 (couse the couple is already in [at] G1 and [at] L1);
Unique have to be: beta,11 (couse the other couple of value in array
(gamma,12 and gamma) have to be excluded both couse gamma exist in both!
I don't really know how to explaine better, Im just tryng to do what usually
do with a single oracle query and that now i cant do couse i don't have
grants for a dblink..
Tnx again all for the patient with a noob like me :)
Vito
--20cf30050e2cc9ca3e049e0f7825--
Re: multidimensional array check
On 09/03/2011 14:03, vito pascali wrote:
> Tnx a lot for u tips!!
> I just used all (or almost all) u suggestion, but what i really need is some
> help in the "logic" of the script.
> I mean at the moment I have this 3 arrays:
>
> [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
> [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
> [at] G2 =('gamma');
>
> I need to found only the unique couple of results: ["beta" , "11"] , couse
> [gamma 12] and [gamma] have to be excluded.
> What i obtain at the moment is instead:
>
> Overlap:
> $VAR1 = [
> 'alfa',
> '10'
> ];
> ### Correct!! #########
>
> Unique:
> $VAR1 = [
> 'gamma' #### Not correct ########
> ];
> $VAR2 = [
> 'gamma', #### Not correct ########
> '12'
> ];
> $VAR3 = [
> 'beta',### Correct!! #########
> '11'
> ];
Can you explain exactly what the your rules are? You seem to be saying
that ["alfa", "10"] and ["alfa", "10"] belong in 'overlap' because they
are identical, which is fair enough. But by what criterion do
["gamma", "12"] and ["gamma"] belong in the same list? Would ["alfa"]
belong there too? And ["alfa", "12"]? And ["gamma", "10"]?
Rob
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--20cf3036408d8e8df8049e0fd19d
Content-Type: text/plain; charset=ISO-8859-1
>
> Can you explain exactly what the your rules are? You seem to be saying
> that ["alfa", "10"] and ["alfa", "10"] belong in 'overlap' because they
> are identical, which is fair enough. But by what criterion do
> ["gamma", "12"] and ["gamma"] belong in the same list? Would ["alfa"]
> belong there too? And ["alfa", "12"]? And ["gamma", "10"]?
>
> Rob
>
Sorry again, I'm really bad to describe the questions.
I'll try again:
[at] G1 = (["alfa" , "10"], ["beta" , "11"]);
[at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
[at] G2 =('gamma');
ok let's che before the first 2 array:
alfa,10 they are identical and so are in overlap group, fine;
so remain beta,11 and gamma,12 ok?
Now let's check these results on [at] G2:
beta,11 is unique and belongs to [at] unique correctly,
gamma,12 have to be excluded couse the couple of value contain gamma.
Is like when u do a select of a row (gamma,12) that is not in other row
which contains just one of the value (specifically col1 = gamma in the
example).
I hope i was clear enough.
tnx again for the patient u all have
Vito
--20cf3036408d8e8df8049e0fd19d--
Re: multidimensional array check
--001517577072a46b47049e0fd34a
Content-Type: text/plain; charset=ISO-8859-1
Can you explain exactly what the your rules are? You seem to be saying
> that ["alfa", "10"] and ["alfa", "10"] belong in 'overlap' because they
> are identical, which is fair enough. But by what criterion do
> ["gamma", "12"] and ["gamma"] belong in the same list? Would ["alfa"]
> belong there too? And ["alfa", "12"]? And ["gamma", "10"]?
>
> Rob
>
Sorry again, I'm really bad to describe the questions.
I'll try again:
[at] G1 = (["alfa" , "10"], ["beta" , "11"]);
[at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
[at] G2 =('gamma');
ok let's che before the first 2 array:
alfa,10 they are identical and so are in overlap group, fine;
so remain beta,11 and gamma,12 ok?
Now let's check these results on [at] G2:
beta,11 is unique and belongs to [at] unique correctly,
gamma,12 have to be excluded couse the couple of value contain gamma.
Is like when u do a select of a row (gamma,12) that is not in other row
which contains just one of the value (specifically col1 = gamma in the
example).
I hope i was clear enough.
tnx again for the patient u all have
Vito
--001517577072a46b47049e0fd34a--
Re: multidimensional array check
On 3/9/11 Wed Mar 9, 2011 8:46 AM, "vito pascali" <vito.pascali [at] gmail.com>
scribbled:
>
> The results of u script are:
>
> Overlap: alfa
> Unique: gamma, beta
> Remains: beta
>
> What I need is really different:
>
> Overlap have to be: alfa,10 (couse the couple is already in [at] G1 and [at] L1);
> Unique have to be: beta,11 (couse the other couple of value in array
> (gamma,12 and gamma) have to be excluded both couse gamma exist in both!
> I don't really know how to explaine better, Im just tryng to do what usually
> do with a single oracle query and that now i cant do couse i don't have
> grants for a dblink..
That sounds only "slightly" different. Please examine the code that I have
provided and modify it for your own, specific needs.
If you want to print out the values along with the keys, then do so! You
will also find the the line labeled "Unique" contains keys that are unique
to [at] G1 and [at] L1 before keys in [at] G2 are eliminated. The line labeled "Remains"
contains the keys left over after that step is done. So it seems to me that
the key, value pairs in the %unique hash are the ones you want. Let us know
if that is not the case.
To print the values along with the keys:
for my $key ( keys %unique ) {
print "[ $key, $unique{$key} ]\n";
}
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: multidimensional array check
--0016e64de50449feeb049e22323a
Content-Type: text/plain; charset=ISO-8859-1
>
> That sounds only "slightly" different. Please examine the code that I have
> provided and modify it for your own, specific needs.
>
U are right, I think I'm really close to the solution of my problem..
I tried to put all the lesson togheter and I got this script that do what i
want:
#!/usr/bin/perl
use strict;
use warnings;
my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
my [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
my [at] G2 =('gamma','teta');
my %unique;
for my $e1 ( [at] G1 ) {
$unique{$e1->[0]} = $e1->[1];
}
my %overlap;
for my $e2 ( [at] L1 ) {
my( $key, $val ) = [at] $e2;
if( exists $unique{$key} ) {
$overlap{$key} = 1;
delete $unique{$key};
}else{
$unique{$key} = $val;
}
}
for my $e3 ( [at] G2 ) {
delete $unique{$e3};
}
for my $key ( keys %unique ) {
print "Unique: ", "[ $key, $unique{$key} ]\n";
}
This script do what I need but still miss the dbi part. So i tried to put
the stuff again with this script:
######################################################
#!/usr/bin/perl -w
use strict;
use warnings;
use DBI;
use DBD::mysql;
use warnings;
my $db_gal = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" )
or die( $DBI::errstr . "\n" );
my $SEL_G1 = "select * from GAL";
my $query_handle_gal = $db_gal->prepare($SEL_G1);
$query_handle_gal->execute();
my $tbl_ary_ref_g1 = $query_handle_gal->fetchall_arrayref();
#########################################
my $db_lab = DBI->connect(
"dbi:mysql:test_lab:localhost:3306","user","password" )
or die( $DBI::errstr . "\n" );
my $SEL_L1 = "select * from LAB";
my $query_handle_lab = $db_lab->prepare($SEL_L1);
$query_handle_lab->execute();
my $tbl_ary_ref_l1 = $query_handle_lab->fetchall_arrayref();
##########################################
my $db_gal2 = DBI->connect(
"dbi:mysql:test_gal:localhost:3306","user","password" )
or die( $DBI::errstr . "\n" );
my $SEL_G2 = "select * from GAL2";
my $query_handle_gal2 = $db_gal2->prepare($SEL_G2);
$query_handle_gal2->execute();
my $tbl_ary_ref_g2 = $query_handle_gal2->fetchall_arrayref();
#########################################
#my [at] G1 = (["alfa" , "10"], ["beta" , "11"]);
#my [at] L1 = (["alfa" , "10"], ["gamma" , "12"]);
#my [at] G2 =('gamma','teta');
# populate a hash with the elements of G1
my %unique;
for my $e1 ( [at] $tbl_ary_ref_g1 ) {
$unique{$e1->[0]} = $e1->[1];
}
# add elements in L1 not in G1
# delete elements in both
my %overlap;
for my $e2 ( [at] $tbl_ary_ref_l1 ) {
my( $key, $val ) = [at] $e2;
if( exists $unique{$key} ) {
$overlap{$key} = 1;
delete $unique{$key};
}else{
$unique{$key} = $val;
}
}
for my $e3 ( [at] $tbl_ary_ref_g2 ) {
delete $unique{$e3};
}
for my $key ( keys %unique ) {
print "Unique: ", "[ $key, $unique{$key} ]\n";
The 3 queries give these results from the cli:
1)
mysql> select * from GAL;
+------+------+
| col1 | col2 |
+------+------+
| alfa | 10 |
| beta | 11 |
+------+------+
2)
mysql> select * from LAB;
+-------+------+
| col1 | col2 |
+-------+------+
| gamma | 12 |
| alfa | 10 |
+-------+------+
3)
mysql> select * from GALILEO2;
+-------+
| col1 |
+-------+
| gamma |
+-------+
Now the script don't do what i was expecting couse it gives me:
Unique: [ gamma, 12 ]
Unique: [ beta, 11 ]
Where i was looking for:
Unique: [ beta, 11 ]
Sure i'm really bad at programmer, bbut really i think im not so far from
the results needed....
Tnx again everybody for the patient, really!
Vito
--0016e64de50449feeb049e22323a--