Subject: | dpath( .. )->match() returning odd result for regex-based value search |
run the attached script.
on a data struct defined as:
we try to pick the elements using the DPath expressions:
//*[ value =~ /i/ ]
//*[ value =~ /f/ ]
oddly enough, when the regex searches for /i/, the result include the entire hashref ponted to by the 'aHash' key in the first level. See output's excerpt below:
----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here -----
::: elements with letter 'i'
on a data struct defined as:
my $data = {
aList => [qw/aa bb cc dd ee ff gg hh ii jj/],
aHash => {
apple => 'pie',
banana => 'split',
potato => [qw(baked chips fries fish&chips mashed)],
},
};
//*[ value =~ /i/ ]
//*[ value =~ /f/ ]
oddly enough, when the regex searches for /i/, the result include the entire hashref ponted to by the 'aHash' key in the first level. See output's excerpt below:
----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here -----
::: elements with letter 'i'
[
[0] "split",
[1] "pie",
[2] "ii",
[3] "chips",
[4] "fries",
[5] "fish&chips",
[6] { >>>>>>>> why this hashref is being returned here??? makes no sense
apple "pie",
banana "split",
potato [
[0] "baked",
[1] "chips",
[2] "fries",
[3] "fish&chips",
[4] "mashed"
]
}
]
::: elements with letter 'f'
[
[0] "ff",
[1] "fries",
[2] "fish&chips"
]
----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here ----- cut here -----
Subject: | test-data-dpath.pl |
#!/usr/bin/perl
use 5.010;
use Data::Printer colored => 0;
use Data::DPath qw/dpath dpathr/;
my $data = {
aList => [qw/aa bb cc dd ee ff gg hh ii jj/],
aHash => {
apple => 'pie',
banana => 'split',
potato => [qw(baked chips fries fish&chips mashed)],
},
};
sub pick {
my ( $caption, $dpath ) = @_;
say '::: ' . $caption;
my @res = dpath($dpath)->match($data);
p @res;
}
# so
say 'we all';
p $data;
############################################################################
pick( 'list', '/aList' );
pick( 'list element', '/aList/*[2]' );
pick( 'all 4th elements of all lists', '//*[3]' );
say '::: tweaking list element';
my @elem = dpathr('/aList/*[2]')->match($data);
say @elem;
${ $elem[0] } = 'WAKAWAKAWAKA';
p @elem;
pick( 'list after tweaking', '/aList' );
############################################################################
pick( 'hash', '/aHash' );
say 'hash address: ' . $data->{aHash};
pick( q{elements with letter 'i'}, '//*[ value =~ /i/ ]' );
pick( q{elements with letter 'f'}, '//*[ value =~ /f/ ]' );