Perl:如何访问通过引用传递给子例程的 3 个散列内部的数组
Perl: How to access an array thats inside of 3 hashes passed to subroutine by reference
我有一个代码是这样的:
foreach $item (@total_data)
{
setinfo($item);
} # @total_data contains an array of references to hashes (\%hash1 ... \%hashN)
在子程序中是这样的:
sub setinfo
{
my ($argument) = @_;
my $i = 0;
#inside original hash $argument{"data"}{"fulldraw"} there is an [array]
#that contains numbers of the form XYYZ and I want to split them into
#the following pairs XY YY YZ but that code works ok#
foreach $item (${$argument{"data"}{"fulldraw"}})
{
my $match;
my $matchedstr;
if ($item =~ /^\d{4}$/)
{
...
}
else
{
print STDERR "DISCARDED: $item\n";
}
}
}
我知道我可能在取消引用它的方式上犯了错误,但是我在互联网上阅读的所有文章都无法弄清楚。
谢谢!
只需使用解引用 @{ ... }
:
foreach $item (@{ $argument->{data}{fulldraw} })
您可以使用 Data::Dumper 可视化复杂结构:
use Data::Dumper;
print Dumper($argument);
@{ ... } # dereference
也许 $argument 是一个 hashref;你需要使用
foreach $item (@{ $argument->{data}->{fulldraw} })
我有一个代码是这样的:
foreach $item (@total_data)
{
setinfo($item);
} # @total_data contains an array of references to hashes (\%hash1 ... \%hashN)
在子程序中是这样的:
sub setinfo
{
my ($argument) = @_;
my $i = 0;
#inside original hash $argument{"data"}{"fulldraw"} there is an [array]
#that contains numbers of the form XYYZ and I want to split them into
#the following pairs XY YY YZ but that code works ok#
foreach $item (${$argument{"data"}{"fulldraw"}})
{
my $match;
my $matchedstr;
if ($item =~ /^\d{4}$/)
{
...
}
else
{
print STDERR "DISCARDED: $item\n";
}
}
}
我知道我可能在取消引用它的方式上犯了错误,但是我在互联网上阅读的所有文章都无法弄清楚。
谢谢!
只需使用解引用 @{ ... }
:
foreach $item (@{ $argument->{data}{fulldraw} })
您可以使用 Data::Dumper 可视化复杂结构:
use Data::Dumper;
print Dumper($argument);
@{ ... } # dereference
也许 $argument 是一个 hashref;你需要使用
foreach $item (@{ $argument->{data}->{fulldraw} })