I'm trying to search a nested structure and return an array with references to all occurrences that match something like:
->{foo}->{bar}
or
->{foo}->[0]->{bar}
My recursive function (below) works fine for single instances like ->{bar} but somehow creates an endless loop with multiple parts (i.e ->{foo}->{bar}). I think it has something to do with the eval corrupting $data but I dont understand why or how to fix it. Any help would be appreciated.
The function:
sub rec_data {
my $path = shift; # ->{foo}->{bar}
my $data = shift; # nested perl structure
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}) {
push @results, rec_data($path, $data->{$k});
}
}
}
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) {
push @results, rec_data($path, $k);
}
}
}
return @results;
}
# test data
my $data = {
test => {
foo => { bar => 'bar', baz => 'baz' },
bar => { baz => 'baz' },
}
}
# search
my @results = rec_data('->{foo}->{bar}', $data);
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|