Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This question already has an answer here:

Is is possible to assign {x = 2, y = 3, z = 4} to a variable var so that one can write

Block[var, x*y*z]

(or similar) instead of

Block[{x = 2, y = 3, z = 4}, x*y*z]


share|improve this question

marked as duplicate by Mr.Wizard Apr 26 '13 at 2:02

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

If I remember right, I think there is a similar question with an excellent answer by @LeonidShifrin . Just can't recall the keywords right now.. – Silvia Apr 25 '13 at 20:11
@Silvia I answered a similar one here - perhaps, that's what you meant? One of the solutions I posed does use the injector pattern, although by that time it did not yet have this nice name :-) – Leonid Shifrin Apr 25 '13 at 20:21
@LeonidShifrin It's this one I meant, looks like a bad memory, alerts me to go to sleep now :/ – Silvia Apr 25 '13 at 20:24
While this is a simpler question than the linked duplicate as it does not start with strings, it is a subset of that question and also answered there. If anyone feels that this should not be closed please vote accordingly or respond to this comment. – Mr.Wizard Apr 26 '13 at 2:04
@Leonid long overdue, but I finally referenced that answer in my own. At the time of writing I did not recall your use. – Mr.Wizard Apr 26 '13 at 2:29
up vote 5 down vote accepted

This could be another case for the injector pattern:

var = Hold@{x = 2, y = 3, z = 4}

var /. Hold[inj_] :> Block[inj, x*y*z]

(*Global`x --- so we did not leak*)
share|improve this answer

Here's something I found:

With[{h := {x = 7, y = 8}},
 Block[h, x y]]


share|improve this answer
That's an excellent application of this AFAIK-undocumented syntax. My old "injector pattern" answer was given specifically to handle cases where this method did not work (Sequence); in cases where it does it's surely easier to read for those not already acquainted with the "injector" or deeply familiar with replacement rules. +1 – Mr.Wizard Apr 26 '13 at 2:07
It should be noted that this does not use var but the Set expressions explicitly. Nevertheless it is instructive and my +1 stands. – Mr.Wizard Apr 26 '13 at 2:17
var /: Block[var, code_] := Block[{x = 2, y = 3, z = 4}, code]


x = 100;
Block[var, x + 2]

(* 4 *)
share|improve this answer

The way I prefer is

var := {x = 2, y = 3, z = 4}
ReleaseHold[Hold[Block[var, x*y*z]] /. OwnValues[var]]

-> 24

-> x


Apply[Block, Hold[var, x*y*z] /. OwnValues[var]]

-> 24

-> x


hVars = Hold[{x = 2, y = 3, z = 4}];

We can do

ReleaseHold[Hold[Block][hVars, Hold[x*y*z]]]

-> 24

-> x

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.