Perl MySQL查询访问超出数组';受约束

Perl MySQL查询访问超出数组';受约束,mysql,perl,Mysql,Perl,我有以下过程,用于同步两个MySQL表: sub sync{ my %tables = ( 'sitematrix_test' => 'sitematrix_sites' ); while(($key, $value) = each(%tables)){ print "Matching columns $key : $value\n"; my @data; my $query = $db->p

我有以下过程,用于同步两个MySQL表:

sub sync{
    my %tables = (
        'sitematrix_test' => 'sitematrix_sites'
    );

    while(($key, $value) = each(%tables)){
        print "Matching columns $key : $value\n";

        my @data;
        my $query = $db->prepare("
            SELECT $key.* FROM $key LEFT JOIN $value ON 
            $key.site_id = $value.site_id WHERE $value.site_id IS NULL;
        ") 
            or die "Couldn't prepare statement: " . $db->errstr;

        $query->execute() 
            or die "Couldn't execute statement: " . $query->errstr;

        if($query->rows == 0){
            print "No updates have been made for $key : $value";
        }
        else{
            #read the matching records and print them out
            while(@data = $query->fetchrow_array()){
                print "@data\n";
            }
        }

        $query->finish;
    }

    $db->disconnect;    
}
它给出了以下错误:

Use of uninitialized value $data[3] in join or string at C:/Users/souzamor/workspace/Parser/synchronizer.pl line 69.
Use of uninitialized value $data[4] in join or string at C:/Users/souzamor/workspace/Parser/synchronizer.pl line 69.
Use of uninitialized value $data[5] in join or string at C:/Users/souzamor/workspace/Parser/synchronizer.pl line 69.
Use of uninitialized value $data[6] in join or string at C:/Users/souzamor/workspace/Parser/synchronizer.pl line 69.

有人能解释一下为什么它超出了数组的界限吗?

您的数据库数据中有
NULL
s;它们在Perl中被转换为
undef
。警告来自这一行:

print "@data\n";
在这里,您可以对数组进行字符串化。例如:

perl -Mwarnings -e '@foo=(1, 2, undef, 3); print "@foo\n"'
Use of uninitialized value $foo[2] in join or string at -e line 1.
1 2  3
如果您真的希望字符串化整个数组并每次打印它,一个简单的解决方案是将
unde
s转换为空字符串:

while( my @data = map { defined($_) ? $_ : '' } $query->fetchrow_array() ) {

或者,如果您不需要打印每一行的所有数据,只需打印一个主键或一些您知道不会为
NULL

的内容,您也可以使用,而不是带有
defined
的三元组。我不认为这样是正确的:
while(my@data=map{defined?$\u:“”$query->fetchrow\u array())
@simbabque你能演示一下吗?@philippe,我更新了答案以解决
地图中的语法问题。正如simbabque指出的,您还可以使用
映射{$\uu/''}