【发布时间】:2021-09-23 22:45:44
【问题描述】:
我将一个哈希 %attributes 分配给另一个哈希 %attributes_r。我需要将其打印为该哈希 %attributes 的键/值对,如下所述。但是,在打印时,为什么我会收到这个错误,“Can't use string ("") as a HASH ref while "strict refs" in use at" in this line "foreach my $key1 (keys %{$attributes_r{ $key}}) {" ?
我的代码:
use strict;
use warnings;
our %attributes_r;
my %attributes = ('clear' => 0,
'reset' => 0,
'bold' => 1,
'dark' => 2,
'underscore' => 4,
'blink' => 5,
'reverse' => 7,
'concealed' => 8
);
for (keys %attributes) {
$attributes_r{$attributes{$_}} = $attributes{$_};
# print "$_ => $attributes_r{$attributes{$_}}\n";
}
foreach my $key (keys %attributes_r) {
foreach my $key1 (keys %{$attributes_r{$key}}) {
print "$key1 = > $attributes_r{$key}{$key1}\n";
}
}
感谢任何帮助。
【问题讨论】:
-
$_是关键,所以$attributes_r{$attributes{$_}} = $attributes{$_};应该是$attributes_r{$_} = $attributes{$_};。也许你应该给循环变量一个名字以避免混淆... /// 第二个循环没有意义。 -
您能否编辑问题以显示您希望新的
$attributes_r看起来像什么?我的意思是,尽可能准确地展示它——理想情况下,你会向我们展示该哈希应该是什么。