How to copy an array using a link in Perl?

If I do the following, it works fine:

print $ref->{element}->[0]->{data}; 

I would like to see how many links are in the array so that I can iterate over them, but it's hard for me to do this.

Here is the code I tried but it does not work:

 my @array = @$ref->{element}; foreach(@array) { print $_->{data}; } 

I get the error message "Not ARRAY reference"

+4
source share
2 answers

List hashes are complex in this way. @$ref->{element} parsed as (@$ref)->{element} , dereferencing $ref instead of $ref->{element} .

Try

 my @array = @{$ref->{element}} 

or

 my $size = scalar @{$ref->{element}} 

Details in perllol .

+8
source

As a general debugging help, give Data :: Dumper . This is invaluable for peering into internal data structures.

-2
source

Source: https://habr.com/ru/post/1301044/


All Articles