RE: Printing Variables and Their Values
- To: mathgroup at smc.vnet.net
- Subject: [mg46418] RE: Printing Variables and Their Values
- From: "David Park" <djmp at earthlink.net>
- Date: Tue, 17 Feb 2004 07:05:49 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Bruce,
It's probably a matter of style, but I wouldn't do it that way.
First I would hardly ever write a statement like x = 7 unless I had a
Clear[x] in the same Cell. Single letter symbols are just too often used as
pure symbols and the most common error is to set values for them and then
forget that you did that.
I would set up the data as a set of substitution rules.
(data = {a -> 1, b -> 2, c -> 3, x -> 7, y -> 5, z -> 4}) // TableForm
Then you can perform all the algebraic manipulations you want and substitute
the data at any point without destroying your ability to use x,y,z etc., as
symbols.
To display a limited set of values you could use...
displayvalues[symbols___] :=
Thread[{symbols} == ({symbols} /. data)] // TableForm
displayvalues[a, c, x, z]
Or if you want to print the data across a line you could use
printvalues[symbols___] :=
MapAt[Drop[#, -1] &,
Inner[SequenceForm[ToString[#1] <> " = ", #2,
", "] &, {symbols}, {symbols} /. data, SequenceForm], -1]
printvalues[x, y, z]
The Inner statement is always a nice way to combine two equal length lists,
in this case the symbols and the symbol values. The Drop statement was
needed to get rid of the comma and space on the last value.
David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/
From: Bruce W. Colletti [mailto:bcolletti at compuserve.com]
To: mathgroup at smc.vnet.net
Re Mathematica 5.0.1.
For a list of variables, how would I successively print each variable's
name followed by its value?
For instance, when x = 7, y = 5 and z = 4, feed list {x,y,z} into
a function that prints "x = 7, y = 5, z = 4."
Although Print["x = ", x, " y = ", y, " z = ", z] works, I want
to automate this for any list.
Thanks.
Bruce