Post Reply 
How to remove an item from a list? [SOLVED]
10-22-2016, 07:52 AM (This post was last modified: 10-23-2016 06:29 AM by cclinus.)
Post: #1
How to remove an item from a list? [SOLVED]
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!
Find all posts by this user
Quote this message in a reply
10-22-2016, 09:07 AM
Post: #2
RE: How to remove an item from a list?
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;
Find all posts by this user
Quote this message in a reply
10-22-2016, 09:28 AM (This post was last modified: 10-22-2016 09:37 AM by StephenG1CMZ.)
Post: #3
RE: How to remove an item from a list?
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.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
10-22-2016, 10:02 AM (This post was last modified: 10-22-2016 10:04 AM by DrD.)
Post: #4
RE: How to remove an item from a list?
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-
Find all posts by this user
Quote this message in a reply
10-22-2016, 11:23 AM (This post was last modified: 10-22-2016 11:59 AM by cclinus.)
Post: #5
RE: How to remove an item from a list?
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!
Find all posts by this user
Quote this message in a reply
10-22-2016, 11:53 AM (This post was last modified: 10-22-2016 11:54 AM by CH3791.)
Post: #6
RE: How to remove an item from a list?
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).
Find all posts by this user
Quote this message in a reply
10-22-2016, 01:15 PM
Post: #7
RE: How to remove an item from a list?
(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-
Find all posts by this user
Quote this message in a reply
10-23-2016, 06:28 AM
Post: #8
RE: How to remove an item from a list?
Hi Dale,

Thanks! It works.

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

Regards,
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 2 Guest(s)