MathGroup Archive 2000

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

Search the Archive

Re: How to call by reference for particular argument(s)?

  • To: mathgroup at
  • Subject: [mg22116] Re: [mg22093] How to call by reference for particular argument(s)?
  • From: Hartmut Wolf <hwolf at>
  • Date: Wed, 16 Feb 2000 02:34:32 -0500 (EST)
  • Organization: debis Systemhaus
  • References: <>
  • Sender: owner-wri-mathgroup at

Wen-Feng Hsiao schrieb:
> 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?
> Wen-Feng

To begin with the last, i'd code it more like:

u1 = {a, b, c, d, e, {b, d}}; u2 = {b, d};

Position[u1, #] & /@ u2

{{{2}, {6, 1}}, {{4}, {6, 2}}}

I redefined u1 a little bit, to show that this method is much stronger,
i.e. it gives better information. If you need your output in a different
form, it seems to be better to postprocess that!

Now to your function. It works if you undo the Hold for your first and
second argument:

u1 = {a, b, c, d, e}; u2 = {b, d};

res = {}; myPosition[Evaluate[u1], Evaluate[u2], res];


{2, 4}

An alternative would be to make your reference (res) be the first
argument of your function, and giving that the attribute HoldFirst. But,
as I said, it's better in many respects to stick as close as possible to
standard conventions and standard functionality.

Kind regards, Hartmut

  • Prev by Date: Re: Simple Problem (I suppose...)
  • Next by Date: Re: Re: Sorting
  • Previous by thread: How to call by reference for particular argument(s)?
  • Next by thread: Re: How to call by reference for particular argument(s)?