erlug
[Top] [All Lists]

[Erlug] array multidimensionali in perl

To: erlug@xxxxxxxxxxxxxx
Subject: [Erlug] array multidimensionali in perl
From: vito pascali <vito.pascali@xxxxxxxxx>
Date: Mon, 14 Mar 2011 18:00:32 +0100
Ciao a tutti, scusate se chiedo un qualcosa di programmazione non strettamente inerente a linux ma non ne sto riuscendo a venire fuori e non so più a chi chiedere.

Il problema è che devo trovare le coppie di valori uniche confrontando i risultati fra 3 array che provengono a loro volta 3 query effettuate a due database server diversi.
Questo script fa esattamente quello che voglio partendo dai 3 array senza la parte dbi di accesso al database:

#!/usr/bin/perl
use strict;
use warnings;

my @G1 = (["alfa" ,  "10"], ["beta" ,  "11"]);
my @L1 = (["alfa" ,   "10"], ["gamma", "12"]);
my @G2 =('gamma');


my %unique;
for my $e1 ( @G1 ) {
 $unique{$e1->[0]} = $e1->[1];
}

my %overlap;
for my $e2 ( @L1 ) {
 my( $key, $val ) = @$e2;
 if( exists $unique{$key} ) {
   $overlap{$key} = 1;
   delete $unique{$key};
 }else{
   $unique{$key} = $val;
 }
}


for my $e3 ( @G2 ) {
 delete $unique{$e3};
}


for my $key ( keys %unique ) {
 print "Unique: ", "[ $key, $unique{$key} ]\n";
}

e mi restituisce come voluto:

Unique: [ beta, 11 ]

Ho quindi provato a metter su la parte dbi nello stesso script in questa maniera:

##############################
########################

#!/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();
#########################################

# populate a hash with the elements of G1
my %unique;
for my $e1 ( @$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 ( @$tbl_ary_ref_l1 ) {

 my( $key, $val ) = @$e2;
 if( exists $unique{$key} ) {
   $overlap{$key} = 1;
   delete $unique{$key};
 }else{
   $unique{$key} = $val;
 }
}


for my $e3 ( @$tbl_ary_ref_g2 ) {
 delete $unique{$e3};

}

for my $key ( keys %unique ) {
 print "Unique: ", "[ $key, $unique{$key} ]\n";



Le 3 query danno questi risultati dalla 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 GAL2;
+-------+
| col1  |
+-------+
| gamma |
+-------+

Qui  invece mi restituisce due coppie, in pratica non mi cancella la coppia che contiene i valori presi dalla query G2.

Unique: [ gamma, 12 ]
Unique: [ beta, 11 ]

Suggerimenti RTFM link e trikketrakke son ben accetti, veramente
Vito

<Prev in Thread] Current Thread [Next in Thread>