Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #7884 > unrolled thread
| Started by | burke <burket@mailbox.sc.edu> |
|---|---|
| First post | 2012-01-16 12:02 +0000 |
| Last post | 2012-01-17 10:57 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
opposite of AppendTo burke <burket@mailbox.sc.edu> - 2012-01-16 12:02 +0000
Re: opposite of AppendTo "Nasser M. Abbasi" <nma@12000.org> - 2012-01-17 08:31 +0000
Re: opposite of AppendTo "Nasser M. Abbasi" <nma@12000.org> - 2012-01-17 08:41 +0000
Re: opposite of AppendTo Szabolcs Horvát <szhorvat@gmail.com> - 2012-01-17 10:57 +0000
| From | burke <burket@mailbox.sc.edu> |
|---|---|
| Date | 2012-01-16 12:02 +0000 |
| Subject | opposite of AppendTo |
| Message-ID | <jf13lb$5ma$1@smc.vnet.net> |
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!!
[toc] | [next] | [standalone]
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2012-01-17 08:31 +0000 |
| Message-ID | <jf3bke$n92$1@smc.vnet.net> |
| In reply to | #7884 |
On 1/16/2012 6:02 AM, 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.
> Help!!
>
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
[toc] | [prev] | [next] | [standalone]
| From | "Nasser M. Abbasi" <nma@12000.org> |
|---|---|
| Date | 2012-01-17 08:41 +0000 |
| Message-ID | <jf3c7i$ngs$1@smc.vnet.net> |
| In reply to | #7884 |
On 1/16/2012 3:57 PM, Nasser M. Abbasi wrote:
> On 1/16/2012 6:02 AM, 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.
>> Help!!
>>
>
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
>
> 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
[toc] | [prev] | [next] | [standalone]
| From | Szabolcs Horvát <szhorvat@gmail.com> |
|---|---|
| Date | 2012-01-17 10:57 +0000 |
| Message-ID | <jf3k7b$q5t$1@smc.vnet.net> |
| In reply to | #7884 |
On 2012.01.16. 14:02, 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. > Help!! > 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web