Comments (13)
Not sure yet if I correctly understand what the criteria for 'street2' is. You already selected the [3] element, in which street2 appears. You could specify that criteria further like
my $query_path = '/*/*[2]/.[value eq "street"]/../*[3]/.[value eq "street2"]';
But that's probably not helpful when you already know it in advance, so not sure...
from data-dpath.
I want to select the 2nd array item, via index.
my $query_path = '/*/*[2]/.[value eq "street"]/../*[3]/.[idx == 1]';
but this returns
$VAR1 = [];
while
my $query_path = '/*/*[2]/.[value eq "street"]/../*[3]/.[idx == 0]';
returns
$VAR1 = [
'street1',
'street2'
];
How do I access the array refs first or second item so that I get this result?
$VAR1 = [
'street2'
];
from data-dpath.
Hm, that array containing [street1, street2] is the result of the dpath query, containing everything it found with the dpath somewhere in the data structure. So it's technically not in the data structure and therefore not addressable with the dpath.
So you would query with a dpath like you quite did already:
my $query_path = '/*/*[2]/.[value eq "street"]/../*[3]';
and on the result choose the $result[1]
.
from data-dpath.
I cant use perl code to work with the dpath result, the query needs to return
$VAR1 = [
'street2'
];
is there no query that does that?
from data-dpath.
Not on the result set.
In dpath logic you need to express it from the point of view of the actual data structure.
So you could early on in the path filter out the wrong indexes, like with *[idx > 1]
:
my $query_path = '/*[idx > 1]/*[value eq "street"]/../*[3]';
But still it all comes down to your knowledge about the result in advance.
from data-dpath.
your solution doesnt work for our use case.
I need to select all nodes from $input_pl
that have "street" as the third item. And after that I want to select the 4th item by index.
so
1st step should yield
[ 2, "ATTR", "street", "street1" ],
[ 3, "ATTR", "street", "street2" ]
what if the source data would look like this `["item1","item2"]
is there no query that returns either "item1" or "item2" using an index?
ps: I appreciate your help.
from data-dpath.
I'm quite sure I understand your problem. However, whether that item you want to access is the n-th item depends on the data structure in which you search. You are looking at the result where that "street2" could result from anywhere in the data structure. That gives us 3 options:
-
Either you know that in your your data structure it's the 4th item, then you can encode it into the dpath, like my last proposal.
-
Or, you don't know your data structure and so you can't know whether it's the 4th item.
-
If you don't know the data structure, but know the result, like in your question, then you have to work on that result data. Which is outside the scope of what the dpath iterates.
I can see that you might have a restricted environment where you can only change the dpath but nothing else.
But as you would have to change the dpath's index anyway to reach the 4th result, respectively any other index every time the input data structure changes, then why not do that at the beginning of the dpath as I suggested?
Alternatively, maybe in your environment you can daisy-chain dpath operations and feed the result into the next with /*[3]
?
from data-dpath.
Actually, you already solved that "4th item problem" with your *[3]
part. I'm talking about the "second result item" problem. It's a bit confusing...
from data-dpath.
I will try with another example, because I am having a hard time putting my question into understandable words. :)
my $input = [
[ "foo_1", "bar_1" ],
[ "foo_2", "bar_2" ],
...
[ "foo_n", "bar_n" ],
[ "address", "street1" ],
[ "address", "street2" ],
];
- I dont know how many items are in the array before the "address" items come.
- select items where [0] equals "address"
/*/*[0]/.[value eq "address"]/..
$VAR1 = [
[
'address',
'street1'
],
[
'address',
'street2'
]
];
now select the 2nd item, I cant figure out how to do that, and I feel like I am running in a circle. :p
ps: I suppose this is not a DPath issue but rather my mind not grasping how to use it. my apologies. I dont understand why I can filter out the nodes I want but after that I cant select a specific one by index.
from data-dpath.
I absolutely understand your problem and I feel sorry to not have better answers.
The answer to your last question is: dpath works on the nodes - not on its own result after that. But that is exactly what you want. You want a match on the result of a match so far. That's different data set than the actual input data structure.
So maybe you can extend the application to accept two (or more) dpaths to allow a "chain" of dpath matching.
The first does what you already do now.
The second dpath works on the result of the previous step, in your case with /*[1]
to select street_2.
And possibly more, each referring to the result of the previous step.
We could invent a syntax for DPath "chains", like /this/is/path1 :: /and/this/is/path2
but that's a tricky parsing problem and outside my currently available time budget.
from data-dpath.
One yould invent a "container syntax for dpath chains" which is easier to parse with the currently used balanced expression parser and even recognizable, like this:
{/this/is/path1}{/and/this/is/path2}{/and/even/a/path3}
but still: it doesn't exist yet and I'm really low on time and energy.
from data-dpath.
I think I understood the modules constraints now and found a solution for my problem. namely, chaining two path executions as you advised.
thank you for your effort to help me!
from data-dpath.
Nice. Thanks for your patience.
from data-dpath.
Related Issues (9)
- Just a test HOT 3
- Number found where operator expected at {path}/warnings.pm line {line}, near "caller 1 HOT 3
- Name "main::INC" used only once: possible typo HOT 1
- Need path validation method HOT 4
- weird behaviour, newline in $path HOT 20
- Implement a "container syntax for dpath chains" based on some balanced expression like this:
- Explosion of NYTProf cruft HOT 2
- escapes inside filter HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from data-dpath.