How to grab Index instead of relative position using xpath - xpath

Given the following xml:
<randomName>
<otherName>
<a>item1</a>
<a>item2</a>
<a>item3</a>
</otherName>
<lastName>
<a>item4</a>
<a>item5</a>
</lastName>
</randomName>
Running: '//a' Gives me an array of all 5 "a" elements, however '//a[1]' does not give me the first of those five elements (item1). It instead gives me an array containing (item1 and item 4).
I believe this is because they are both position 1 relatively. How can I grab any a element by its overall index?
I would like to be able to use a variable "x" to get itemX.

You can wrap it in parenthesis so it knows to apply the index to the entire result set
(//a)[1]

Related

np.where() with np.linspace()

I am trying to find index of an element in x_norm array with np.where() but it doesn' t work well.
Is there a way to find index of element?
x_norm = np.linspace(-10,10,1000)
np.where(x_norm == -0.19019019)
Np.where works with np.arange() and can find the index either first or last element of array creating by linspace.
The numbers generated by np.linspace contains more decimal places than the one you are pasting to np.where (-0.19019019019019012).
So it might be better to use np.argmin to find the nearest value and avoid rounding errors:
x_norm = np.linspace(-10,10,1000)
yournumber=-0.19019019
idx=np.argmin(np.abs(x_norm-yournumber))
You can then go further and add np.where(x_norm==x_norm[idx]) to your code in case if you'll have array with duplicates.
Set the level of precision to 8 using np.round then use np.where to filter data as a mask then apply the mask to the array.
x_norm = np.round(np.asarray(np.linspace(-10,10,1000)),8)
results=x_norm[np.where(x_norm==-9.91991992)]
print(results)

In Ruby is there a way to get the index of an item in an array that consists of structs?

With a normal array, I can use the arrayname.find_index('whatimlookingfor') to get the position within the array.
I can't figure out how to do this when the elements of the array are Struct's.
Scenario: I have a struct that consists of an ID and the Filename. In one function I need to find within that array the ID of a different file than the one I'm currently processing. I know the other filename, so what I was hoping that I could do something like:
arrayname.filename.find_index(parsedfilename)
But this obviously fails. Without iterating through the entire array is there a way to quickly reference the index of where the match happens? Or am I out of luck because the array is a collection of structs?
index (same as find_index) takes a block in which you can code up any true/false logic for your finder. To find the index of the first item whose filename does not match parsedfilename...
found_index = items.index { |item| item.filename != parsedfilename }
Many methods which work with Arrays and Enumerables also take blocks.

How can I select the second last item in a xpath query?

I'm new to xpath and I understand how to get a range of values in xpath:
/bookstore/book[position()>=2 and position()<=10]
but in my case, I need to get above 2 and one less then the total(so if there's 10 then I need 9, or if there's 5, I need up to the 4th spot). I'm applying my code to different pages and the number of entries is not always the same.
In python, I could do something like book[2:-2], but I'm unsure if I can do this within xpath.
You can use last() which represents the last item in the context:
/bookstore/book[position()>=2 and position() <= (last() - 1)]
In my case this was working for me to get last but one element
/bookstore/book[position() = (last() - 1)]

how can I get the location for the maximum value in fortran?

I have a 250*2001 matrix. I want to find the location for the maximum value for a(:,i) where i takes 5 different values: i = i + 256
a(:,256)
a(:,512)
a(:,768)
a(:,1024)
a(:,1280)
I tried using MAXLOC, but since I'm new to fortran, I couldn't get it right.
Try this
maxloc(a(:,256:1280:256))
but be warned, this call will return a value in the range 1..5 for the second dimension. The call will return the index of the maxloc in the 2001*5 array section that you pass to it. So to get the column index of the location in the original array you'll have to do some multiplication. And note that since the argument in the call to maxloc is a rank-2 array section the call will return a 2-element vector.
Your question is a little unclear: it could be either of two things you want.
One value for the maximum over the entire 250-by-5 subarray;
One value for the maximum in each of the 5 250-by-1 subarrays.
Your comments suggest you want the latter, and there is already an answer for the former.
So, in case it is the latter:
b(1:5) = MAXLOC(a(:,256:1280:256), DIM=1)

How to get the last element of a sequence in XPath?

In Ruby we can access an array with negative numbers like array[-1] to get the last object in the array. How do I do this using XPath?
I can't do this:
result = node.xpath('.//ROOT/TAG[-1]/KEY_NAME')
I found a solution here on Stack Overflow, but that is a query that just changes the upper limit to get elements. This could return one last item or last item and prevous.
What if I want to get only the prevous element like array[-2] in Ruby?
You can access the last element in XPath using last() in a predicate.
node.xpath('.//ROOT/TAG[last()]/KEY_NAME')
And use [last()-1] for the second-to-last position.

Resources