HP Forums

Full Version: Hp prime list function - indexing into a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

I cannot figure out how to iterate through a list extracting individual items.

The POS function searches a list for a particular value but I don't want that - I want to get the exact 1st, 2nd etc. items of a list. I'm a bit surprised such a function doesn't exist? I mean, if you think of a list as an array, there seems no way to be able to get to item 1 or item 2 etc. which you would think are fundamental operations. Am I missing something?

Quote:
I cannot figure out how to iterate through a list extracting individual items.

The POS function searches a list for a particular value but I don't want that - I want to get the exact 1st, 2nd etc. items of a list. I'm a bit surprised such a function doesn't exist? I mean, if you think of a list as an array, there seems no way to be able to get to item 1 or item 2 etc. which you would think are fundamental operations. Am I missing something?

listname(pos) -- an example using the built-in list name L1 would be L1(2) to get the second item in the list.

Also, L1(1,2,3) would access "4" in this list: {{1,{2,3,4}},5}. For a matrix, M1(3,2) would access item in 3rd row, 2nd col. Storage also functions similarly.

TW

Edited: 6 Oct 2013, 10:44 a.m.

Quote:
Also, L1(1,2,3) would access "4" in this list: {{1,{2,3,4}},5}. For a matrix, M1(3,2) would access item in 3rd row, 2nd col. Storage also functions similarly.

and of course it also works the other way around:
L1(1):= value changes the first value of L1.
L1(1,2,3):=v does as you would expect...
L1(10):=v if L1 is of size <10 will increase the size as needed.
L1(0):= val will ADD val at the end of the list (so you do not have to do L1(SIZE(L1)+1):= val

cyrille