Autovivification!
When you search for a path, if its depth is greater than 1, your main data under search will grow, and that's exactly why your program will not stop.
Now I think it is your duty to fix it.
I added some debug info at certain key points, to reveal that your data is growing itself, see my inline comments: (Run it and watch the chain of foo->{foo}->{foo}->...->{})
use Data::Dumper; sub rec_data { my $path = shift; # ->{foo}->{bar} my $data = shift; # nested perl structure my $data_main = shift; print Dumper($data_main);#let's see the fact my @results; # nothing to search return unless ref $data; # stop the evals complaining no warnings 'all'; if (ref $data eq 'HASH') { foreach my $k (keys %$data) { my $ref = qq|\$data->{$k}$path|; if (my $val = eval($ref)) { push @results, ref($val) ? $val : eval "\\$ref"; } if (ref $data->{$k}) { print "calling from entry point1, k = $k\n"; push @results, rec_data($path, $data->{$k}, $data_main +);#just pass the third parameter along without change(?) } } } elsif (ref $data eq 'ARRAY') { foreach my $k (@$data) { my $ref = qq|\$k$path|; if (my $val = eval($ref)) { push @results, ref($val) ? $val : eval "\\$ref"; } if (ref $k) { print "calling from entry point2\n"; push @results, rec_data($path, $k, $data_main); } } } return @results; } my $data_main = { test => { foo => { bar => 'bar', baz => 'baz' }, bar => { baz => 'baz' } , } }; my @results = rec_data('->{foo}->{bar}', $data_main, $data_main);#The +program should never change the third parameter, is that true? print Dumper(\@results);
In reply to Re: searching nested structures
by pg
in thread searching nested structures
by IOrdy
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |