当我尝试推荐存储的引用(在提取Tripwire / get Data子例程中设置)并将其转换回哈希(在Compare子例程中)时,即%hash =%{$ DataHash {$ key我尝试打印键。我遇到了这些问题:
在数组取消引用中使用未初始化的值$ hash {“ElementName”} ... 这是在@hashItems = @ {$ hash {ElementName}};行
在打印时使用未初始化的值.... print“Data:”,$ hash {ElementName},“\ n”;线
sub extract_tripwire{
foreach $server (@servers){
$server = lc $server;
my $xlfile = "";
$xlfile .= "";
# Open the provided Excel sheet
my $book = $xl->Workbooks->Open($xlfile);
# Setip active worksheet
my $sheet = $book->Worksheets(1);
# Finds the Last row that has data in it
my $LastRow = $sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByRows})->{Row};
my $LastCol = $sheet->UsedRange->Find({What=>"*",
SearchDirection=>xlPrevious,
SearchOrder=>xlByColumns})->{Column};
print "Last Row: $LastRow, Last Column: $LastCol\n";
#This will be a reference to a hash
if($LastRow > 1){
my %Data = %{&get_Data($LastRow,$LastCol,$sheet)};
}
else{
print "No program Changes\n";
}
# Close the workbook when done
$xl->ActiveWorkbook->Close(0);
#Maybe Store a reference to the hash?
$DataHash{$server} = \%Data;
}
# Get the names of the columns from the given excel file
sub get_Data{
# Initialization of variables
my @header;
my @data;
my %Data;
# Print out all the data
my $array = $_[2]->Range("A1:I1")->{'Value'};
# Cycle through A1->I1 , A1->B1->C1->D1...
foreach my $ref_array (@$array){
foreach my $scalar(@$ref_array){
push @header, $scalar;
}
}
#The letters that are associated with the columns of the excel file
my @letters = ('A','B','C','D','E','F','G','H','I');
my $counter = 0;
# Loop through all the columns and store the data within a hash with the specific header as its key
for($index = 0; $index < scalar @letters; $index++,$counter++){
# The range of the column
my $array = $_[2]->Range("$letters[$index]2:$letters[$index]$_[0]")->{'Value'};
# Cycle through A2->I4 , A2->A3->A4->...->I2->...
foreach my $ref_array (@$array){
foreach my $scalar(@$ref_array){
if(defined($scalar)){
push @data, $scalar;
}
else{
$scalar = " ";
}
}
}
$Data{$header[$counter]} = @data;
@data = ();
}
# Return the data hash
return \%Data;
}
&Compare(\%DataHash);
}
sub Compare{
# Get the hash of a has that was created from the tripwire reports
my %Datahash = %{$_[0]};
# Get the keys of that file
@keys = sort keys %DataHash;
foreach(@keys){
print "Keys: $_\n";
}
#Loop through the keys
foreach my $key (@keys){
----------> #This is the Main PROBLEM in the code
----------> %hash = %{$DataHash{$key}};
# Get all the keys of that hash
@hashKeys = keys %hash;
print "Data: ", $hash{ElementName}, "\n";
#Get the items Programs that has been implemented
@hashItems = @{$hash{ElementName}};
#Try and match them up against the Task from Alfresco
for($i = 0; $i < scalar @hashItems;$i++){
for($j = 0; $j < scalar @promoCode; $j++){
#Split up the PromoCode Here!!!! 0- File name, 1- Task #
#If a match has been found
if($hashItems[$i] ~~ $promoCode[$j]){
# Extract the row that match
foreach (@hashKeys){
@array = @{$hash{$_}};
#And store it in this array
push @pass, $array[$i];
}
# So that the information can be passed to the Reconcile routine to be added to the Details and Summary reports + Task #
&Reconcile(@pass,$i);
#Need insert and empty row
$sheet->Range("A$lastRow:G$LastRow")->Select;
$xl->Selection->Interior->{ColorIndex} = 15;
$xl->Selection->Interior->{Pattern} = xlSolid;
}
}
}
}
}'
我是如何创建哈希哈希的?我怎么读它?
答案 0 :(得分:2)
如果这是关于调试代码的,我可以建议使用Data::Dumper打印您的哈希吗?它应该为您提供足够的信息来解决问题。
use Data::Dumper;
print Dumper \%hash;
答案 1 :(得分:0)
有一点是这句话:
$Data{$header[$counter]} = @data;
会将数组@data的大小分配给哈希值,可能不是你想要的那样!
分配您需要执行此操作的数组:
@{ $Data{$header[$counter]} } = @data
答案 2 :(得分:0)
这是不必要的:
foreach my $scalar(@$ref_array){
push @header, $scalar;
}
更简单地说:
push @header, @$ref_array;
但至于你的大问题,第88行(按我的计算):
&Compare(\%DataHash);
由于未定义%DataHash
- 您可能没有strict
。这意味着它创建了一个包变量%main::DataHash
,它是一个空哈希并传递对该空哈希的引用。因此,它仅表示$DataHash{ElementName}
为undef
。
由于您一直在处理%Data
,您可能想要这样做:
Compare( \%Data );
你不需要像打电话那样使用&符号。
所以这是USUW - use strict; use warnings;