需要从Perl中的变量元素的散列数组构建foreach语句

时间:2021-03-10 21:23:01

I'm using XML::Simple in Perl to parse through an XML file and I'm stuck on how to build a loop to go through all the possible elements of an array within the hash.

我在Perl中使用XML::Simple解析XML文件,我还在研究如何构建一个循环来遍历散列中数组的所有可能元素。

Here's how to print the 0th element of the array:

下面是如何打印数组的第0个元素:

print $book_info->{BookList}->{BookData}->{Prices}->{Price}[0]->{is_new};

My terminology might be off when using the words hash vs. array, but I'm trying to loop through all of the elements within {Price}[$ref]

我的术语在使用hash vs. array时可能会出错,但我试图遍历{Price}[$ref]中的所有元素

I tried:

我试着:

my @refs = $book_info->{BookList}->{BookData}->{Prices}->{Price};
foreach(@refs)
{
    print $book_info->{BookList}->{BookData}->{Prices}->{Price}[$_]->{store_id};
    print "\n";
}


and

foreach my $key (keys (%{$book_info->{BookList}->{BookData}->{Prices}->{Price}}))
{
    print $key."\n";
}


This next print statement returns a value of "ARRAY(0x159a57c)"

下一个print语句返回“ARRAY(0x159a57c)”

   print [$book_info->{BookList}->{BookData}->{Prices}->{Price}];


This works for the foreach, but I can't access the elements correctly:

foreach (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
    print $book_info->{BookList}->{BookData}->{Prices}->{Price}[$_]; #this line is wrong
}

Any suggestions? There are multiple {Price} elements within the {Prices} element, and each {Price} element has [x] attributes in the XML.

有什么建议吗?{Price}元素中有多个{Price}元素,每个{Price}元素在XML中都有[x]属性。

3 个解决方案

#1


3  

People aren't quite getting it right for you. If I understand you correctly, you want:

人们不太适合你。如果我理解正确,你想:

foreach my $element ( @{ $book_info->{BookList}->{BookData}->{Prices}->{Price} } ) {
    print $element->{store_id};
    print "\n";
}

Helpful advice for dealing with nested data structures can be found at http://perlmonks.org/?node=References+quick+reference

可以在http://perlmonks.org/?node=References+quick+reference找到处理嵌套数据结构的有用建议

#2


2  

Marginally extending ysth's answer:

略微延长ysth的回答是:

foreach my $elem_ref (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
    foreach my $key (sort keys %{$elem_ref})
    {
        print $elem_ref->{$key};
    }
    print "\n";
}

In part, this uses the recommendation from Perl Best Practices to denote references with the suffix _ref.

在某种程度上,这使用了Perl最佳实践中的推荐,用后缀_ref表示引用。

#3


0  

Did you try dumping the contents of $book_info with Data::Dumper (use Data::Dumper; print Dumper($book_info)? It should give you a clue on how to proceed.

您是否尝试将$book_info的内容与数据一起转储::Dumper(使用数据::Dumper;打印翻车机(book_info美元)?它应该会给你一个如何继续的线索。

#1


3  

People aren't quite getting it right for you. If I understand you correctly, you want:

人们不太适合你。如果我理解正确,你想:

foreach my $element ( @{ $book_info->{BookList}->{BookData}->{Prices}->{Price} } ) {
    print $element->{store_id};
    print "\n";
}

Helpful advice for dealing with nested data structures can be found at http://perlmonks.org/?node=References+quick+reference

可以在http://perlmonks.org/?node=References+quick+reference找到处理嵌套数据结构的有用建议

#2


2  

Marginally extending ysth's answer:

略微延长ysth的回答是:

foreach my $elem_ref (@{$book_info->{BookList}->{BookData}->{Prices}->{Price}})
{
    foreach my $key (sort keys %{$elem_ref})
    {
        print $elem_ref->{$key};
    }
    print "\n";
}

In part, this uses the recommendation from Perl Best Practices to denote references with the suffix _ref.

在某种程度上,这使用了Perl最佳实践中的推荐,用后缀_ref表示引用。

#3


0  

Did you try dumping the contents of $book_info with Data::Dumper (use Data::Dumper; print Dumper($book_info)? It should give you a clue on how to proceed.

您是否尝试将$book_info的内容与数据一起转储::Dumper(使用数据::Dumper;打印翻车机(book_info美元)?它应该会给你一个如何继续的线索。