我正在尝试解决使用
创建哈希的问题push @{ $test{$onecell2}{$onecell3}{$onecell4} }, $onecell1;
之所以使用它,是因为前3个重复了多个值,即oncell1。
我无法打印哈希,因为在第四级时出现此错误:Nòt a HASH reference
如果有人可以帮助,那就太好了。谢谢
my %test;
push @{ $test{$onecell2}{$onecell3}{$onecell4} }, $onecell1;
foreach my $name (sort keys %test) {
foreach my $subject (keys %{ $test{$name} }) {
foreach my $storage (keys %{ $test{$name}{$subject} }) {
foreach my $size (keys %{ $test{$name}{$subject}{$storage} }) {
print "$name: $subject: $storage: $size \n";
}
}
}
}
错误是:Not a HASH reference
这是哈希结构
$VAR1 = {
'A1RE' => {
'Recombinant Human' => {
"Lyophilized protein " => [
'10 ug',
'50 ug',
'500 ug',
'1 mg'
]
}
};
我希望输出为:
AIRE: Recombinant Human: Lyophilized protein: 10 ug,50 ug, 500 ug, 1mg.
答案 0 :(得分:1)
因为最后一级是数组引用,而不是哈希。您需要在数组引用上使用数组取消引用@{ ... }
:
#!/usr/bin/perl
use strict;
use warnings;
my %test = (
'A1RE' => {
'Recombinant Human' => {
"Lyophilized protein " => [
'10 ug',
'50 ug',
'500 ug',
'1 mg',
],
},
},
);
foreach my $name (sort keys %test) {
my $subjects = $test{$name};
foreach my $subject (keys %{ $subjects }) {
my $storages = $subjects->{$subject};
foreach my $storage (keys %{ $storages }) {
my $sizes = $storages->{$storage};
print "$name: $subject: $storage: ", join(", ", @{ $sizes }), "\n";
}
}
}
exit 0;
输出:
$ perl dummy.pl
A1RE: Recombinant Human: Lyophilized protein : 10 ug, 50 ug, 500 ug, 1 mg