The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Hp prime list function - indexing into a list
Message #1 Posted by Andy B (Australia) on 6 Oct 2013, 9:30 a.m.

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?

      
Re: Hp prime list function - indexing into a list
Message #2 Posted by Han on 6 Oct 2013, 9:32 a.m.,
in response to message #1 by Andy B (Australia)

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.

            
Re: Hp prime list function - indexing into a list
Message #3 Posted by Tim Wessman on 6 Oct 2013, 10:43 a.m.,
in response to message #2 by Han

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.

                  
Re: Hp prime list function - indexing into a list
Message #4 Posted by cyrille de Brébisson on 7 Oct 2013, 1:41 a.m.,
in response to message #3 by Tim Wessman

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


[ Return to Index | Top of Index ]

Go back to the main exhibit hall