Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

opposite of AppendTo

226 views
Skip to first unread message

burke

unread,
Jan 16, 2012, 7:02:51 AM1/16/12
to
AppendTo[s,e] appends "e" to "s" and then RESETS "s". How does one
delete or remove an element "e" from a list "s" and then RESET "s"?
The _reset_ is my problem. I want to be able to add and delete
elements of a list (a list of graphical elements with very low
opacity). I can "appendto" the list at will but am unable to delete
elements (using Delete, DeleteCases, Except, Select, etc.). Deleting
"e" from "s" is easy enough, but resetting "s" seems impossible.
Help!!

Nasser M. Abbasi

unread,
Jan 17, 2012, 3:31:10 AM1/17/12
to
I do not know if there is such a command now, looked and can't see
one (this does not mean there is not one).

But it is possible to write your own myDelete function which does
the resetting and it would look the same as the AppendTo. You just
need to pass the list to the function myDelete by reference to do this,
but it will now work by resetting the list.

Like this

-----------------
ClearAll[myDelete]
myDelete[from_, item_] := Module[{loc},
loc = Position[from, item];
If[Not[loc === {}], from = Delete[from, loc]]
];
------------

Now you can use it like this:

s = {1, 2};
e = 2;
myDelete[Unevaluated@s, e];
s
===> {1}


s = {1, 2, 3, 2};
e = 2;
myDelete[Unevaluated@s, e];
s
===> {1, 3}


s = {1, 2, {3}, 2};
e = {3};
myDelete[Unevaluated@s, e];
s
===>{1, 2, 2}

and so on.

So, you just need to do
1. replace Delete with myDelete
2. add Unevaluated@ to the list

use at your own risk. Not tested very well. Not responsible for
any damage caused by the use of this myDelete function.

--Nasser

James Stein

unread,
Jan 17, 2012, 3:36:47 AM1/17/12
to
list = Drop [ list , -1 ]

DrMajorBob

unread,
Jan 17, 2012, 3:40:21 AM1/17/12
to
Show us an example of the failure of Delete, DeleteCases, Except, and/or
Select.

We can't just GUESS what you've tried.

Bobby
--
DrMaj...@yahoo.com

Nasser M. Abbasi

unread,
Jan 17, 2012, 3:41:22 AM1/17/12
to
On 1/16/2012 3:57 PM, Nasser M. Abbasi wrote:
> On 1/16/2012 6:02 AM, burke wrote:
btw, if you do not like to use Unevaluate@ every time to pass something by
reference, then you can use the following version instead:

-------------------------
ClearAll[myDelete]
myDelete[from_, item_] := Module[{loc},
loc = Position[from, item];
If[Not[loc === {}], from = Delete[from, loc]]
];
SetAttributes[myDelete, HoldFirst];
------------------------------

and now can just call it as

---------------------------
s = {1, 2};
e = 2;
myDelete[s, e];
s
---------------------------

===> {1}

This might be easier to use, since you do not need to add Unevaluated@
each time.

--Nasser

Tomas Garza

unread,
Jan 17, 2012, 3:43:56 AM1/17/12
to
If I understand correctly, your problem seems to lie in the use of AppendTo. This is a very tricky function, since it "resets" s without your control and sometimes things get messed up. Perhaps you could try to use simply Append, where you get a new list but s is not changed in the process. Essentially, what AppendTo does is s = Append[s,e].
-Tomas

> Date: Mon, 16 Jan 2012 17:12:16 -0500
> From: bur...@mailbox.sc.edu
> Subject: opposite of AppendTo
> To: math...@smc.vnet.net

Bill Rowe

unread,
Jan 17, 2012, 3:48:31 AM1/17/12
to
On 1/16/12 at 5:12 PM, bur...@mailbox.sc.edu (burke) wrote:

>AppendTo[s,e] appends "e" to "s" and then RESETS "s". How does one
>delete or remove an element "e" from a list "s" and then RESET "s"?
>The _reset_ is my problem. I want to be able to add and delete
>elements of a list (a list of graphical elements with very low
>opacity). I can "appendto" the list at will but am unable to delete
>elements (using Delete, DeleteCases, Except, Select, etc.). Deleting
>"e" from "s" is easy enough, but resetting "s" seems impossible.

In[12]:= a = data = RandomReal[1, 10];
AppendTo[data, 100];
data = Most[data];
a == data

Out[15]= True


Bob Hanlon

unread,
Jan 17, 2012, 3:51:05 AM1/17/12
to
s = Array[x, 4];

AppendTo[s, e]

{x[1], x[2], x[3], x[4], e}

s = Most[s]

{x[1], x[2], x[3], x[4]}

AppendTo[s, e]

{x[1], x[2], x[3], x[4], e}

s = Drop[s, -1]

{x[1], x[2], x[3], x[4]}

AppendTo[s, e]

{x[1], x[2], x[3], x[4], e}

s = s /. e -> Sequence[]

{x[1], x[2], x[3], x[4]}


Bob Hanlon

On Mon, Jan 16, 2012 at 5:12 PM, burke <bur...@mailbox.sc.edu> wrote:
> AppendTo[s,e] appends "e" to "s" and then RESETS "s". How does one
> delete or remove an element "e" from a list "s" and then RESET "s"?
> The _reset_ is my problem. I want to be able to add and delete
> elements of a list (a list of graphical elements with very low
> opacity). I can "appendto" the list at will but am unable to delete
> elements (using Delete, DeleteCases, Except, Select, etc.). Deleting
> "e" from "s" is easy enough, but resetting "s" seems impossible.
> Help!!
>

Szabolcs Horvát

unread,
Jan 17, 2012, 5:57:47 AM1/17/12
to
You can always re-assign the list, as in

s = Most[s];

In Mathematica functions almost never modify their arguments directly.
They return a modified copy instead.

Exceptions are Set and AppendTo/PrependTo, which do modify the argument.

--
Szabolcs Horvát
Mma QA site proposal: http://area51.stackexchange.com/proposals/37304

Fred Simons

unread,
Jan 17, 2012, 5:58:48 AM1/17/12
to
Is this what you are looking for?

In[8]:= Attributes[mydelete]={HoldFirst};
mydelete[s_, e_] := s=DeleteCases[s,e]

In[10]:= s=Range[5];
mydelete[s,3];
s
Out[12]= {1,2,4,5}

Fred Simons
Eindhoven University of Technology

Op 16-1-2012 23:12, burke schreef:
0 new messages