HP Forums

Full Version: How to remove an item from a list? [SOLVED]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

This is my list: {0, {0,1,2},{0,1,2}}

I want to remove the last item {0,1,2} from the list but keep the first two items.
How?

end result should be {0, {0,1,2}}

I had read some command such as DELCOL(only work with matrix), remove(can not keep one {0,1,2} in the list.

Thanks!
I don't think there is a built-in command to remove an item from a list.
Here is a small program to do it :
Code:
EXPORT Remove(l,n)    //remove item n from list l
BEGIN
 LOCAL s:=SIZE(l);
 CASE
  IF n==1 THEN l({2,s}) END;
  IF n==s THEN l({1,s-1}) END;
  IF n>1 AND n<s THEN CONCAT(l({1,n-1}),l({n+1,s})) END;
  DEFAULT l END;
END;
http://www.hpmuseum.org/forum/thread-7001.html
is also a useful library of list-handling procedures in the software library.

Suggested enhancement to the syntax:
To remove an item from the end of the list, the simplest and most obvious syntax would be
SIZE(Let):=SIZE(Let)-1
But SIZE(Let):=
Is not currently supported.
You could use this syntax:

{0, {0,1,2},{0,1,2}}; // Original list
SUB({0, {0,1,2},{0,1,2}},1,2); // End result desired ==> {0, {0,1,2}}

-Dale-
Hi Dale,

Thanks! But it is partially work.
If I assign a:={0,{0,1,2},{0,1,2}}
Then SUB(a,1,2)
IN Home mode , it is ok.
But if in CAS mode, It display Error: Invalid dimension.
How to make it work in CAS mode?

FINALLY, I find a way to make it work in CAS mode.
EXPR("SUB(a,1,2)");

TOO BAD, it seems doesn't work in cas program when "a" is local variable.

Thanks!
Perhaps you can also consider a CAS program?
http://www.hpmuseum.org/forum/thread-3590.html

If you can always access/mostly use CAS, I think they are a much better choice than normal programs. It's more straightforward to program and it accepts your input with all the symbolic representation (ie doesn't convert things like pi to decimal).
(10-22-2016 11:23 AM)cclinus Wrote: [ -> ]If I assign a:={0,{0,1,2},{0,1,2}}
Then SUB(a,1,2)

IN Home mode , it is ok.
But if in CAS mode, It display Error: Invalid dimension.
How to make it work in CAS mode?

Try using lower case sub instead of SUB, in CAS:

sub(a,1,2); ==> end result in CAS.

Also, you might like to use list variables L0..L9.

-Dale-
Hi Dale,

Thanks! It works.

sub(a,1,2); //OK

Regards,
Reference URL's