Assigning multiple variables within a Block[] statement (e.g. {a,b} = {c,d})
- To: mathgroup at smc.vnet.net
- Subject: [mg88283] Assigning multiple variables within a Block[] statement (e.g. {a,b} = {c,d})
- From: "news.purdue.edu" <dnquark at yahoo.com>
- Date: Wed, 30 Apr 2008 07:02:27 -0400 (EDT)
- Organization: Purdue University
What I'm trying to do:
I have a list of parameters that I am using across several functions. I am
trying to come up with a good way to avoid copying/pasting these parameter
assignments everywhere. Here is an example code that sort of accomplishes
this goal: (Suppose my parameters are a,b, and c)
paramlist = {a -> 1, b -> 4, c -> 8};
paramlistnames = First /@ paramlist;
foobar2[x_] := Block[Evaluate[Flatten[{paramlistnames, {d, e, f}}]],
{a, b, c} = paramlistnames /. paramlist;
{d,e,f} = {1,1,1};
x*a*b*c*d*e*f
]
The line
{a, b, c} = paramlistnames /. paramlist;
effectively sets a, b, and c to their values, at which point I can proceed
with the rest of the function.
Questions:
1. Is this the most effective way to accomplish the task at hand? If no,
what is?
2. Note that I still have to explicitly use {a, b, c} = paramlistnames /.
paramlist; something like Hold[paramlistnames] = paramlistnames /.
paramlist; results in an error message. Is there a way to do the assignment
but use the "paramlistnames" somehow so that I don't have to name the
variables explicitly?
Thanks,
--Leo