有人能帮我理解一下我在这里做错了什么吗?
#!/usr/bin/perl
use MIME::Lite;
use Sys::Hostname;
use strict;
use Time::Local;
use warnings;
my %resultMap=();
my @myarray = (
'a',
'b',
'c',
'd'
);
my %ha = (
A => {'UserNum' => 1, 'Password' => 'abc', 'Server' => 'AAAA', 'Database' => 'BBB'},
B => {'UserNum' => 2, 'Password' => 'abc', 'Server' => 'AAAA', 'Database' => 'BBB'}
);
for my $region ( keys %ha ) {
my %hashone=();
foreach (@myarray) {
my $myStr = $_ ;
$hashone {$myStr} = $ha{'UserNum'};
print "$myStr ---> $hashone{$myStr}\n";
}
$resultMap{$region} = { %hashone };
}
for my $regionKey (keys %resultMap ){
print "Key -$regionKey\n";
for my $table ( keys %{ $resultMap{$regionKey} } ) {
my %counthash = $resultMap{$regionKey};
print "$regionKey : $counthash{$table}\n";
}
}我真的不知道我在这里做错了什么,我期望输出的数字,而它是打印后的错误。
Use of uninitialized value in concatenation (.) at testHash.pl line 29.
a --->
Use of uninitialized value in concatenation (.) at testHash.pl line 29.
b --->
Use of uninitialized value in concatenation (.) at testHash.pl line 29.
c --->
Use of uninitialized value in concatenation (.) at testHash.pl line 29.
d --->
Use of uninitialized value in concatenation (.) at testHash.pl line 29.
a --->
Use of uninitialized value in concatenation (.) at testHash.pl line 29.发布于 2015-07-30 14:25:09
这是因为你有一个比你想象的更深层次的嵌套结构。我已经用# <-- here标记了行,我在其中做了一些修改。第二个更改(在第39行上)是因为$resultMap{$regionKey}本身包含一个散列,因此要复制它,您需要用散列的圆周操作符(%{})包围它。
#!/usr/bin/perl
use MIME::Lite;
use Sys::Hostname;
use strict;
use Time::Local;
use warnings;
my %resultMap=();
my @myarray = (
'a',
'b',
'c',
'd'
);
my %ha = (
A => {'UserNum' => 1, 'Password' => 'abc', 'Server' => 'AAAA', 'Database' => 'BBB'},
B => {'UserNum' => 2, 'Password' => 'abc', 'Server' => 'AAAA', 'Database' => 'BBB'}
);
for my $region ( keys %ha ) {
my %hashone=();
foreach (@myarray) {
my $myStr = $_ ;
$hashone{$myStr} = $ha{$region}->{'UserNum'}; # <-- here
print "$myStr ---> $hashone{$myStr}\n";
}
$resultMap{$region} = { %hashone };
}
for my $regionKey (keys %resultMap ){
print "Key -$regionKey\n";
for my $table ( keys %{ $resultMap{$regionKey} } ) {
my %counthash = %{$resultMap{$regionKey}}; # <-- here
print "$regionKey : $counthash{$table}\n";
}
}https://stackoverflow.com/questions/31726151
复制相似问题