如何比较perl中数组引用的值?

时间:2016-08-29 10:34:09

标签: perl dbi fetchall

#!/usr/bin/perl -C0
use strict;
use warnings;   
use DBI;

sub agg_verification
{
my ($list,$tblname,$values);
my $res1=undef;
my $res2=undef;

    #First DB Connection******
    my $connstr = "ENG=xxx;CommLinks=tcpip{port=xxx};UID=xxx;PWD=xxx";
    my $dbh = DBI->connect( "DBI:SQLAnywhere:$connstr", '', '', {AutoCommit => 0} ) or warn $DBI::errstr;
    my $stmt="select col1||':'||col3 as a, col2||'('||col3||')' as b from table where col1 like '%xxxx:((15))%'"; #making 'a' as primary key for fetchall_hashref() later.
    my $sth=$dbh->prepare($stmt) or warn $DBI::errstr;
    $sth->execute() or warn $DBI::errstr;

    #Second DB Connection******
    my $connstr1 = "ENG=xxx;CommLinks=tcpip{port=xxx};UID=xxx;PWD=xxx";
    my $dbh1 = DBI->connect( "DBI:SQLAnywhere:$connstr1", '', '', {AutoCommit => 0} ) or warn $DBI::errstr;
    my ($sth1,$stmt1,$stmt2);
    #creating, opening and writing the result in a file 
       open my $fh, '+>>', "/eniq/home/dcuser/output.txt" or warn "Cannot open output.txt: $!";

       my $res = $sth->fetchall_hashref('a');

       foreach my $key(sort keys %$res) {
            my @col1 = $res->{$key}->{'a'};
            $list=\@col1;
                foreach my $item(@$list)
                    {
                        my @values=${$res}{$item}->{'b'};
                        $values=\@values;
                        my @tblname=$item=~ m/[(\w*)(\:)](DC\w*)/; #trimming table name
                        $tblname =\@tblname;
                        #print $fh "TABLENAME :@$tblname\n";

                            foreach my $raw(@$tblname) #Extracting _RAW data*********************
                            {
                                $raw.="_RAW";
                                chomp($raw);
                                $stmt1 = "select @$values from $raw";
                                $stmt2 = "";
                                $sth1=$dbh1->prepare($stmt1) or warn $DBI::errstr;
                                $sth1->execute() or warn $DBI::errstr;
                                my $max_rows1 = 5_000;
                                $res1 = $sth1->fetchall_arrayref(undef,$max_rows1);

                                                }
                            foreach my $day(@$tblname) #Extracting _DAY DATA******************** 
                            {
                                $day  =~ s/(\_RAW)//;
                                chomp($day);
                                $day .="_DAY";
                                $stmt2 = "select @$values from $day";
                                $sth1=$dbh1->prepare($stmt2) or warn $DBI::errstr;
                                $sth1->execute() or warn $DBI::errstr;
                                my $max_rows = 5_000;
                                $res2 = $sth1->fetchall_arrayref(undef,$max_rows);                              
                                                }

                                if(@$res1 == @$res2)
                                {
                                    print $fh "PASS at @$values\n";
                                                }
                                else
                                {
                                    print $fh "FAIL at @$values\n";
                                                }
                                                    }
                                                        }


    close $fh or warn "Couldn't close file properly";
        $sth1->finish();
        $sth->finish();
        $dbh1->disconnect;
        $dbh->disconnect;
}
agg_verification();

伙计们,我想比较$ res1和$ res2中的数值但是我没有得到它传递或失败的结果。无论变化如何,我的输出都是“通过”。请建议如何在不使用外部CPAN库的情况下比较上面代码中数组引用的值,因为我没有更新或添加库的权限。

1 个答案:

答案 0 :(得分:2)

  

无论变化如何,我的输出都是“通过”

这是因为以下条件,它只是比较两个去引用数组中的元素数。

if(@$res1 == @$res2)

如果你想比较两个数组引用的内容(根据你的问题是数字)那么你可以做

#!/usr/bin/perl
use strict;
use warnings;
use Test::More 'no_plan';
my $res1 = [5,8,10,12];
my $res2 = [5,8,10,12];
is_deeply( $res1, $res2, 'Compare arrayref' );

以上案例的输出:

chankey@pathak:~/Desktop$ perl test.pl 
ok 1 - Compare arrayref
1..1

如果他们不相等,那么:

my $res1 = [5,8,10,12];
my $res2 = [3,8,10,12];

然后,您将获得以下详细输出,您可以轻松检查哪个值不相等

chankey@pathak:~/Desktop$ perl test.pl 
not ok 1 - Compare arrayref
#   Failed test 'Compare arrayref'
#   at test.pl line 7.
#     Structures begin differing at:
#          $got->[0] = '5'
#     $expected->[0] = '3'
1..1
# Looks like you failed 1 test of 1.

我向您展示了一种如何比较2个arrayref的方法,您可能希望看到以下问题中回答的其他方法(只需确保先取消引用数组然后进行比较):

不使用任何模块的解决方案

#!/usr/bin/perl
use strict;
use warnings;
my $res1 = [5,8,10,12];
my $res2 = [3,8,10,12];
foreach my $index (0..$#{$res1}){
    if ($res1->[$index] == $res2->[$index]){
        print "Index: $index, Equal: YES";
    }
    else{
        print "Index: $index, Equal: NO";
        print " [Expected: $res1->[$index], GOT: $res2->[$index]]";
    }
    print "\n";
}

输出:

chankey@pathak:~/Desktop$ perl test.pl 
Index: 0, Equal: NO [Expected: 5, GOT: 3]
Index: 1, Equal: YES
Index: 2, Equal: YES
Index: 3, Equal: YES

根据聊天讨论:

由于$ res1和$ res2的每个索引都有arrayrefs。因此,您应该使用以下内容:

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

my $res1 = [[5],[4],[3],[2]];
my $res2 = [[5],[4],[3],[1]];
foreach my $index (0..$#{$res1}){
    foreach my $inner_index (0..$#{$res1->[$index]}){
        if ($res1->[$index]->[$inner_index] == $res2->[$index]->[$inner_index]){
            print "Equal!! expected: $res1->[$index]->[$inner_index] got: $res2->[$index]->[$inner_index]\n" ;
        }
        else{
            print "Not Equal!! expected: $res1->[$index]->[$inner_index] got: $res2->[$index]->[$inner_index]\n" 
        }

    }

}