Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Set in Scan

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21908] Re: Set in Scan
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Fri, 4 Feb 2000 02:54:39 -0500 (EST)
  • References: <87aua2$ntf@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Johannes:
Let's take a specific exmple:

{v1, v2, v3} = {{1, 1, 1}, {1, 1, 1, 1}, {1, 1, 1, 1, 1}};

Scan[(#[[3]] = new) &, {v1, v2, v3}]                                   (*1*)

Set::"setps": "\!\({1, 1, 1}\) in assignment of part is not a symbol."
.....

We can make this work (explanation at STEPS TO (*2*) below):

Scan[(#[[3]] = new) &, Unevaluated /@ Hold[v1, v2, v3]]    (*2*)

{v1, v2, v3}

{{1, 1, new}, {1, 1, new, 1}, {1, 1, new, 1, 1}}

HOWEVER, the following may be convenient and quicker if it is a matter of
replacements in a fixed list of lists structure:

vs = {v1, v2, v3};

vs[[All, 3]] = n2;     (*3*)

vs

{{1, 1, n2}, {1, 1, n2, 1}, {1, 1, n2, 1, 1}}


STEPS TO  (*2*)

1) #[[3]]= new& is read as  #[[3]]= (new&), change to  (#[[3]]= new& ).
2) The form  aa[[..]] = bb requires aa to be a symbol to which a value has
been assigned with aa = val (not aa := val). This is because the evalution
works on the stored value. In (*1*)  the vi evaluate and so, for example we
get {1,1,1}[[3]] =new, which does not work.
3) So, why the following?

Scan[(#[[3]] = new) &, Hold[v1, v2, v3]]

Set::"setps": "\!\({1, 1, new}\) in assignment of part is not a symbol."
..........

It seems that Scan ignores Hold. (the mechanism is
(#[[3]]= new)&[v1) \[LongRightArrow] (#[[3]]= new)&[{1,1,1}])
\[LongRightArrow] {1,1,1}[[3]]= new (which fails) )
4) Hence

Scan[(#[[3]] = new) &, Unevaluated /@ Hold[v1, v2, v3]]

Now we get #[[3]]= new)&[Unevaluated[v1]) \[LongRightArrow] v1[[3]] =new
(which works!).


Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565


"Johannes Ludsteck" <ludsteck at zew.de> wrote in message
news:87aua2$ntf at smc.vnet.net...
> Dear MathGroup members
>
> I have to do replacements in very long lists. An efficient way to do
> this is to use
>
> x[[index]]=newElement.
>
> This, however, doesn't work if I want to do replacements for a set of
> lists. If I try to replace the third element in the lists v1, v2 and v3 by
> typing
>
> Scan[#[[3]] = newElement &, {v1,v2,v3}]
> I get the error message
> Set::setps: #1 in assignment of part is not a symbol.
>
> Any suggestions?
> Thanks
>
>
> Johannes Ludsteck
> Centre for European Economic Research (ZEW)
> Department of Labour Economics,
> Human Resources and Social Policy
> Phone (+49)(0)621/1235-157
> Fax (+49)(0)621/1235-225
>
> P.O.Box 103443
> D-68034 Mannheim
> GERMANY
>
> Email: ludsteck at zew.de
>





  • Prev by Date: Re: NDSolve series of ode with equilibrium conditions
  • Next by Date: Not drawing lines at polygon edges, in ParametricPlot3D
  • Previous by thread: Re: Set in Scan
  • Next by thread: Re: Re: Set in Scan