无法在Perl循环中访问哈希查找表

时间:2011-05-17 18:44:20

标签: perl excel hash

我正在编写一个比较两个excel文件数据的Perl脚本。

我目前正在接收一个excel文件的两列并将其存储在哈希中。我用于密钥的数据是电子邮件地址,值只是excel表中的另一个值(不重要)。我只是想将所有电子邮件存储为哈希键,以便我可以遍历另一个('删除')excel文件,并查看原始成员表中是否存在该电子邮件。

我的意图是这样的:

1)创建散列和查找数组:

my %members_list;
foreach my $row (2..$MembersLastRow){
    $members_list{lc($Members->Cells($row,1)->{'Value'})} = lc($Members->Cells($row,2)->{'Value'}); 
}

my @removals_list;
foreach my $row (2..$RemovalsLastRow)
{
    push(@removals_list, lc($Removals->Cells($row,1)->{'Value'}));
}

2)遍历查找数组并检查是否存在哈希:

foreach my $key (sort @removals_list)
{
    print $key;
    if($members_list{$key}){
        print " - MATCH!";
    }
    print "\n";
}

问题在于,当我尝试运行它时,从来没有任何匹配。删除列表中的所有元素都应存储在members_list哈希中。我已经尝试将$ members_list {$ key}打印到屏幕上,然后它会出现"未初始化使用"等等等等等等。

我已经和它斗争了好几天,非常感谢任何帮助。

这是我正在使用的整个代码页!!!

use strict;
use 5.010;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Excel';

$Win32::OLE::Warn = 3;                                # die on errors...

# get already active Excel application or open new
my $Excel = Win32::OLE->GetActiveObject('Excel.Application')
    || Win32::OLE->new('Excel.Application', 'Quit');  
$Excel->{DisplayAlerts}=0;   
# open Excel file

my $Book = $Excel->Workbooks->Open("**FILENAME1**"); 
my $Book2 = $Excel->Workbooks->Open("**FILENAME2**"); 

# You can dynamically obtain the number of worksheets, rows, and columns
# through the Excel OLE interface.  Excel's Visual Basic Editor has more
# information on the Excel OLE interface.  Here we just use the first
# worksheet, rows 1 through 4 and columns 1 through 3.


my $Members = $Book->Worksheets(1);
$Members->{Name} = "Members - Points";
my $Results = $Book->Worksheets(2);
$Results->{Name} = "Perl Results -Saved";

my $Removals = $Book2->Worksheets(1);
$Removals->{Name} = "Removals";

my $count=0;
my %members_list;
my @removals_list;

my $MembersLastRow = &findLastRow($Members);
print "After Members Last Row\n";
my $RemovalsLastRow = &findLastRow($Removals);
print "After Removals Last Row\n";

foreach my $row (2..$MembersLastRow)
{
    next unless defined $Members->Cells($row,1)->{'Value'};
    $members_list{lc($Members->Cells($row,1)->{'Value'})} = lc($Members->Cells($row,2)->{'Value'}); 
}
foreach my $row (2..$RemovalsLastRow)
{
next unless defined $Removals->Cells($row,1)->{'Value'};
push(@removals_list, lc($Removals->Cells($row,1)->{'Value'}));
}

#DO THE VLOOKUP
my $matches=1;
print "Now doing comparisons......\n";
$Results->Cells($matches,1)->{'Value'} = "Removal List";
$Results->Cells($matches,2)->{'Value'} = "Corresponding Member List";


foreach my $key (sort @removals_list)
{
    print $key;
    if($members_list{$key}){
        print " - MATCH!";
    }
    print "\n";
}

print "\nComparisons Complete.\n";

sub findLastRow
{
    $_[0]->UsedRange->Find({What=>"*",
    SearchDirection=>xlPrevious,
    SearchOrder=>xlByRows})->{Row};
}

sub findLastCol
{
    $_[0]->UsedRange->Find({What=>"*", 
    SearchDirection=>xlPrevious,
    SearchOrder=>xlByColumns})->{Column};
}

$Book->Save();

1 个答案:

答案 0 :(得分:2)

如果您要存储的值可能是undef,则需要执行此操作:

if ( exists $members_list{$key} ) {

您确定要访问相同的%members_list哈希吗?它没有超出范围或被后来的my %members_list取代?

这表明了什么:

use Data::Dumper;
$Data::Dumper::Terse = $Data::Dumper::Useqq = 1;
print Data::Dumper::Dumper( { 'removals' => [ sort @removals_list ], 'members' => [ sort keys %members_list ] } );