Re: How can I parse arguments of an expression unevaluated?
- To: mathgroup at smc.vnet.net
- Subject: [mg100953] Re: [mg100894] How can I parse arguments of an expression unevaluated?
- From: Carl Woll <carlw at wolfram.com>
- Date: Thu, 18 Jun 2009 20:46:11 -0400 (EDT)
- References: <200906180848.EAA19681@smc.vnet.net>
Erich Neuwirth wrote:
> What I would like to have is the following:
> I want to give the expression
> MyFunArgs[1, 2, a = 3, b = 4]
> and I would like to get 3 lists,
> the first list has all the arguments which are
> not written as an assignment, {1,2}
> The second list should be a list of strings
> with the names of the variables to be assignes
> {"a","b"}
> and the third list should be the values to be assigned:
> {3,4}
>
> so
> MyFunArgs[1, 2, a = 3, b = 4]
> should produce
> {{1,2},{"a","b"},{3,4}}
>
> How can I accomplish this?
>
>
>
Here's one way:
In[911]:= SetAttributes[MyFunArgs, HoldAll]
MyFunArgs[a__] := {Cases[Hold[a], Except[_Set]],
Cases[Hold[a],
HoldPattern[Set[lhs_, _]] :> SymbolName[Unevaluated[lhs]]],
Cases[Hold[a], HoldPattern[Set[_, rhs_]] :> rhs]}
In[913]:= MyFunArgs[1, 2, a = 3, b = 4]
Out[913]= {{1, 2}, {"a", "b"}, {3, 4}}
Carl Woll
Wolfram Research
- References:
- How can I parse arguments of an expression unevaluated?
- From: Erich Neuwirth <erich.neuwirth@univie.ac.at>
- How can I parse arguments of an expression unevaluated?