HP Forums
How to remove an item from a list? [SOLVED] - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: How to remove an item from a list? [SOLVED] (/thread-7087.html)



How to remove an item from a list? [SOLVED] - cclinus - 10-22-2016 07:52 AM

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!


RE: How to remove an item from a list? - Didier Lachieze - 10-22-2016 09:07 AM

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;



RE: How to remove an item from a list? - StephenG1CMZ - 10-22-2016 09:28 AM

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.


RE: How to remove an item from a list? - DrD - 10-22-2016 10:02 AM

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-


RE: How to remove an item from a list? - cclinus - 10-22-2016 11:23 AM

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!


RE: How to remove an item from a list? - CH3791 - 10-22-2016 11:53 AM

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).


RE: How to remove an item from a list? - DrD - 10-22-2016 01:15 PM

(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-


RE: How to remove an item from a list? - cclinus - 10-23-2016 06:28 AM

Hi Dale,

Thanks! It works.

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

Regards,