HP Forums

Full Version: List commands and functions in HP program language
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to extract or insert an element from or into a list using HP prime programming language? Eg in a list {1,2,3,4,5,6} how would you extract element 4 and put it into a variable?
Code:
local new_variable;
new_variable:=variable[item_number];
I tried this :
local var;
var:={1,2,3,4,5,}[4]

There is Syntax error?
(04-21-2014 08:08 PM)Alfon Wrote: [ -> ]I tried this :
local var;
var:={1,2,3,4,5,}[4]

There is Syntax error?

Well, you do have an extra comma after the 5. Try this:

local var:={1,2,3,4,5};
local item:=var(4);
As for the reasoning as to why it only works once inside a variable, how would one distinguish between "multiply your list times a number" {1,2,3,4,5}*(4) and "get the 4th item in the list" {1,2,3,4,5}(4)?

When it has a name it then becomes a named setter/getter function internally.
Yes this works! Thanks. Looks like you need the () around the element number and the list in a variable. Now how would you insert an element?? Is that possible?
Try the store operator...

var(4):="store"

Or if you prefer, you can use the "store"â–¶var(4) variant.
You may also like to check out the POS command:

nv:={1,2,3,4,5,6};
a:=POS(nv,4);
You may also have nested lists, and can access them as follows:

list:={1,2,3,{4,5}};
item:=list(4,2); this stores 5 into the variable 'item'.
Reference URL's