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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.