Xml 需要从Perl中变量元素的哈希数组构建foreach语句
我在Perl中使用Xml 需要从Perl中变量元素的哈希数组构建foreach语句,xml,arrays,perl,hash,foreach,Xml,Arrays,Perl,Hash,Foreach,我在Perl中使用XML::Simple解析XML文件,我一直在研究如何构建一个循环来遍历散列中数组的所有可能元素 以下是如何打印数组的第0个元素: print $book_info->{BookList}->{BookData}->{Prices}->{Price}[0]->{is_new}; 当使用hash vs.array这个词时,我的术语可能不正确,但我正在尝试循环遍历{Price}[$ref] 我试过: my @refs = $book_info->
XML::Simple
解析XML文件,我一直在研究如何构建一个循环来遍历散列中数组的所有可能元素
以下是如何打印数组的第0个元素:
print $book_info->{BookList}->{BookData}->{Prices}->{Price}[0]->{is_new};
当使用hash vs.array这个词时,我的术语可能不正确,但我正在尝试循环遍历{Price}[$ref]
我试过:
my @refs = $book_info->{BookList}->{BookData}->{Prices}->{Price};
foreach(@refs)
{
print $book_info->{BookList}->{BookData}->{Prices}->{Price}[$_]->{store_id};
print "\n";
}
及
下一个打印语句返回值
“ARRAY(0x159a57c)”
这适用于foreach,但我无法正确访问元素:
foreach (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
print $book_info->{BookList}->{BookData}->{Prices}->{Price}[$_]; #this line is wrong
}
有什么建议吗?在
{Price}
元素中有多个{Price}
元素,每个{Price}
元素在XML中都有[x]
属性。是否尝试使用Data::Dumper转储$book\u info的内容(使用Data::Dumper;print Dumper($book\u info)?它应该给你一个如何继续的线索。你是否尝试用Data::Dumper(使用Data::Dumper;print Dumper($book\u info))转储$book\u info的内容?它应该给你一个如何继续的线索。人们对你的理解不太正确。如果我理解正确,你想要:
foreach my $element ( @{ $book_info->{BookList}->{BookData}->{Prices}->{Price} } ) {
print $element->{store_id};
print "\n";
}
有关处理嵌套数据结构的有用建议,请访问人们对您的理解不太正确。如果我理解正确,您希望:
foreach my $element ( @{ $book_info->{BookList}->{BookData}->{Prices}->{Price} } ) {
print $element->{store_id};
print "\n";
}
有关处理嵌套数据结构的有用建议,请参见边际扩展的回答:
foreach my $elem_ref (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
foreach my $key (sort keys %{$elem_ref})
{
print $elem_ref->{$key};
}
print "\n";
}
在某种程度上,这使用了Perl最佳实践的建议,用后缀\u ref
表示引用,略微扩展了作者的答案:
foreach my $elem_ref (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
foreach my $key (sort keys %{$elem_ref})
{
print $elem_ref->{$key};
}
print "\n";
}
在某种程度上,这使用了Perl最佳实践的建议来表示后缀为
\u ref
的引用,我没有使用XML::Simple,但我假设$book\u info->{BookList}->{BookList}->{BookData}->{Prices}->{Price}
是数组ref,所以您需要像这样取消引用($book\u info->{BookList BookList}->{BookData}>{Prices}->{Price}。还包括您现在从脚本中获得的输出。我尝试了foreach(@($book_-info->{BookList}->{BookData}->{Prices}->{Prices}->{Price}))
并在运算符预期的位置找到了标量…
查看和。我没有使用XML::Simple,但我假定$book_-info->{BookList BookData{Prices}->{Price}
是数组ref,所以您需要像这样取消引用它(($book_-info->{BookList}->{BookData}->{Prices}->{Price}->{code>)
还包括您现在从脚本获得的输出。我尝试了foreach(@($book_-info->{BookList}->{BookData}->{BookData}->{Prices Price}>)
并在运算符期望的位置找到了标量…
查看和。也许您可以在此处发布输出,以便我们可以查看。也许您可以在此处发布输出,以便我们可以查看。我尝试了相同的方法,但我想了解以下元素:打印$book_信息->{BookList}->{BookData}->{Prices}->{Price}[$element]
我现在将浏览该网站打印$book_info->{BookList}->{BookData}->{Prices}->{Price}[$element]->{store_id};
{4从网站:${$hRef->{KEY}[1]$hRef->{KEY}->{KEY}[1]$hRef->{KEY}[1]
看起来最接近我的情况,但不确定它是否适合我直到现在才完全理解你的答案…哈哈,谢谢-这行得通!我也尝试了同样的方法,但我想了解这些元素:print$book\u info->{BookList}->{BookData}->{Prices}->{Price}[$element];
我现在会浏览该网站print$book\u info->{BookList}->{BookData}->{Prices}->{Price}[$element]->{store_id};
{35; 4从网站:${$hRef->{KEY}}[1]$hRef->{KEY}[1]$hRef->{KEY}[1]
看起来最接近我的情况,但不确定它是否适合我到目前为止还没有完全理解你的答案……哈哈,谢谢——这很有效!