Re: How to call by reference for particular argument(s)?
- To: mathgroup at smc.vnet.net
- Subject: [mg22218] Re: How to call by reference for particular argument(s)?
- From: Tobias Oed <tobias at physics.odu.edu>
- Date: Fri, 18 Feb 2000 02:34:58 -0500 (EST)
- Organization: Old Dominion University
- References: <8889e1$c4r@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Wen-Feng Hsiao wrote: > > Dear all, > > I cannot figure out how to call by reference for particular arguments. > Say, I want to design a function, which will return the position list > where the second list locates in the first list. For example, > > If one inputs the following code: > > In[1]:= > res={}; > myPosition[{a, b, c, d, e}, {b, d}, res]; > res > > It outputs {2, 4}. Since the b and d in the second list are in the > positions 2 and 4 of first list. My code is listed below. > > SetAttributes[myPosition, HoldAll]; > myPosition[v1_, {e1_, e2___}, res_Symbol] := Module[{tmp}, > tmp = Flatten[Position[v1, e1]]; > If[Length[tmp] != 0, > AppendTo[res, tmp[[1]]]]; > If[nil[{e2}] =!= nothing, (* if e2 not null *) > myPosition[v1, {e2}, res]]]; > > The difficulties are when I change it to the followings, it cannot work: > > In[10]:= > res={}; > u1={a, b, c, d, e}; u2={b, d}; > myPosition[u1, u2, res]; > res > > the myPosition[u1, u2, res] cannot be resolved. How can I do? Change the > signature to myPosition[v1_Symbol, {e1_Symbol, e2___Symbol}, res_Symbol] > does not work either. It's obvious that the intended call by reference is > only for the third argument "ref", so please show me the rope, thanks! > Furthermore, is there any function exists for the same purpose as > myPosition? I am not sure about what exactly you are trying to do but it seems extreemly clumsy too my. Why not skip the whole myPosition thing and use insted res=Map[First,Position[u1,Apply[Alternatives,u2],1]] which you can write using prefix notation as res=First /@ Position[u1,Alternatives @@ u2,1] which is shorter by 6 chars but certainly less readable. Or at least so I think, cheers Tobias