10
$\begingroup$
a := az + 1
b := bz + 5
list := {a, b}

I'd like mathematica to print

a = az + 1
b = bz + 5

so basically it needs to first print the name of the variable in the list, followed by "=", and followed by the actual content of the variable.

update (1):

so here is something close to what I want

a := az + 1
b := bz + 5
list := {Hold@a, Hold@b}
Column[Table[Print[list[[i]], "=", ReleaseHold@list[[i]]], {i, 1, 2}]]

outputs:

Hold[a]=1+az
Hold[b]=5+bz

However, I don't know how to get rid of Hold[]. I'm also hoping there is a more elegant way.

$\endgroup$
1
  • 1
    $\begingroup$ HoldForm works like Hold except that it isn't printed. Therefore I guess replacing Hold with HoldForm should satisfy your needs. $\endgroup$
    – celtschk
    Commented Apr 27, 2012 at 8:07

3 Answers 3

9
$\begingroup$

Also

SetAttributes[f, HoldAll];
f[x_] := #[[1]] <> "=" <> #[[2]] &@ StringSplit[ToString@Definition@x, ":="]
$\endgroup$
5
  • $\begingroup$ I like the use of Definition which is not new to me but is nonetheless something that I don't usually consider +1. $\endgroup$
    – Andy Ross
    Commented Apr 27, 2012 at 5:42
  • 1
    $\begingroup$ this one is a little hard to understand. can you add more info? # is the argument of a pure function, <> is string joint. What does &@ mean?I can see it starts the pure function. $\endgroup$ Commented Apr 27, 2012 at 5:55
  • 1
    $\begingroup$ @kirill_igum The function #[[1]] <> "=" <> #[[2]] & takes it's argument from the list StringSplit[ToString@Definition@x, ":="] $\endgroup$ Commented Apr 27, 2012 at 5:59
  • 5
    $\begingroup$ StringReplace[ToString[Definition[x]], ":=" -> "="] is simpler :) $\endgroup$
    – rm -rf
    Commented Apr 27, 2012 at 5:59
  • $\begingroup$ @R.M yep. you are right! $\endgroup$ Commented Apr 27, 2012 at 6:01
6
$\begingroup$
StringForm["`` = `` \n`` = ``", HoldForm[a], a, HoldForm[b], b]

or

  StringForm["`` = `` \n`` = ``", Defer[a], a, Defer[b], b]

both give

enter image description here

EDIT: To deal with the az=1 issue noted by belisarius, i steal Andy's OwnValues-based approach with a slight variation:

  SetAttributes[prntHF, {HoldAll, Listable}];
  prntHF[sym_] := (OwnValues[sym] /. 
  {RuleDelayed[Verbatim[HoldPattern][lhs_], rhs_]} 
  :> {HoldForm[lhs], HoldForm[rhs]})

with StringForm

  StringForm[" `1` = `2` \n `3` = `4`\n`5` = `6`", 
  Sequence @@ Sequence @@@ prntHF[{a, b, az}]]

to get

enter image description here

Of course, another version

  SetAttributes[prntHF2, {HoldAll, Listable}];
  prntHF2[sym_] := (OwnValues[sym] /. 
  {RuleDelayed[Verbatim[HoldPattern][lhs_], rhs_]} :> 
  Row[{HoldForm[lhs], " = ", HoldForm[rhs]}])

would be much easier to use:

  Column[prntHF2[{a, b, az}]]
$\endgroup$
9
  • $\begingroup$ HoldForm is a very useful trick for this sort of thing. I'm surprised my own answer didn't include it :) +1 $\endgroup$
    – Andy Ross
    Commented Apr 27, 2012 at 5:44
  • 2
    $\begingroup$ +1 This reminded me why I hate the controlstrings of StringForm[] $\endgroup$ Commented Apr 27, 2012 at 5:49
  • $\begingroup$ @Andy, thank you for the vote. Actually, I was just thinking about OwnValues the same way :) $\endgroup$
    – kglr
    Commented Apr 27, 2012 at 5:49
  • $\begingroup$ mmm does not work if you set az=1 $\endgroup$ Commented Apr 27, 2012 at 5:52
  • $\begingroup$ @belisarius, me too:) One those things that I need to check docs every time I use it. $\endgroup$
    – kglr
    Commented Apr 27, 2012 at 5:55
3
$\begingroup$

This seems to work. I'm doubtful that it is very robust though.

SetAttributes[printVar, {HoldAll, Listable}]

printVar[a_] := Row[{Defer[a], " = ", OwnValues[a][[1, 2]]}]

For example...

Column[printVar[{a,b}]]

==> a = 1 + az
    b =  5 + bz

Edit:

Due to @belisarius' comment regarding setting az to some value.

printVar[a_] := 
 Row[{Defer[a], " = ", 
   OwnValues[a] /. {RuleDelayed[_, expr_]} :> HoldForm[expr]}]

Which is admittedly sort of ugly.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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