在 Perl 中查看哈希的哈希?
当我尝试遵循存储的引用(在提取 Tripwire/get Data 子例程中设置)并将其转换回散列(在比较子例程中)时,即 %hash = %{$DataHash{$key}}; ,然后我尝试打印密钥。我遇到这些问题:
在数组取消引用中使用未初始化的值 $hash{"ElementName"} ... 这是@hashItems = @{$hash{ElementName}};线
在 print at .... 中使用未初始化的值 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;
}
}
}
}
}'
我创建哈希的哈希的方式有问题吗?我读得怎么样?
When I try to deference the stored reference (which is set up in the extract Tripwire/get Data subroutines) and convert it back to a hash(in the Compare subroutine), ie %hash = %{$DataHash{$key}};, and I try to print the keys. I run into these problems:
Use of uninitialized values $hash{"ElementName"} in array dereference at line...
This is at @hashItems = @{$hash{ElementName}}; lineUse of uninitialized value in print at .... print "Data: ", $hash{ElementName}, "\n"; line
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;
}
}
}
}
}'
Is there a problem with how i'm creating the hash of hash? How i'm reading it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果这是关于调试您的代码,我是否建议使用 Data::Dumper 来打印你的哈希值?它应该为您提供足够的信息来解决问题。
If this is about debugging your code, might I suggest using Data::Dumper to print your hash? It should provide you with enough information to sort out the kinks.
一方面,这个语句:
将数组 @data 的大小分配给哈希值,可能不是您想要的!
要分配数组,您需要执行以下操作:
Well for one thing this statement:
will assign the size of the array @data to the hash value, probably not what you intended!
to assign the array you need to do this:
这是不必要的:
因为它更简单地说:
但至于你的大问题,第 88 行(根据我的计数):
因为
%DataHash
未定义 - 你可能没有严格
。这意味着它创建一个包变量%main::DataHash
,它是一个空哈希并传递对该空哈希的引用。因此它仅认为$DataHash{ElementName}
将为undef
。由于您一直在处理
%Data
,您可能想要这样做:您不需要像调用 sub 那样使用 & 符号。
所以这是 USUW -
use strict;使用警告;
This is needless:
as it is more simply stated:
But as for your big problem, line 88 (by my count):
Because
%DataHash
is not defined--you probably don't havestrict
on. Which means that it creates a package variable%main::DataHash
, which is an empty hash and passes a reference to that empty hash. So it only figures that$DataHash{ElementName}
would beundef
.Since you've been dealing with
%Data
, you probably wanted to do this:You don't need the ampersands the way you're calling the sub.
So this is a USUW -
use strict; use warnings;