Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2004
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

RE: Printing Variables and Their Values

  • To: mathgroup at smc.vnet.net
  • Subject: [mg46424] RE: [mg46410] Printing Variables and Their Values
  • From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
  • Date: Tue, 17 Feb 2004 07:05:56 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

>-----Original Message-----
>From: Bruce W. Colletti [mailto:bcolletti at compuserve.com]
To: mathgroup at smc.vnet.net
>Sent: Tuesday, February 17, 2004 5:44 AM
>To: mathgroup at smc.vnet.net
>Subject: [mg46424] [mg46410] Printing Variables and Their Values
>
>
>
>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
>

To show a bit of variation:
 
In[1]:= {x, y, z} = {7, 5, 4};

In[2]:= SetAttributes[{myprint1, myprint2}, HoldAll]

In[3]:= myprint1[stuff_List] :=
  Print @@ 
    MapThread[#1 <> " = " <> ToString[#2] <> "\n" &, {ToString /@ 
          Unevaluated /@ Unevaluated[stuff], stuff}]

In[4]:= myprint1[{x, y, z}]
>From In[4]:=
 x = 7
 y = 5
 z = 4

In[6]:= myprint2[stuff__Symbol] :=
  Print[StringDrop[
      StringJoin @@ 
        MapThread[#1 <> " = " <> ToString[#2] <> "; " &,
              {ToString /@ {Unevaluated /@ 
                  Unevaluated[stuff]}, {stuff}}], -2]]

In[7]:= myprint2[x, y, z]
>From In[7]:=
 x = 7; y = 5; z = 4


The usability of these 2 proposals, however, is not too far reaching. You
always have to feed the myprint functions with the unevaluated symbols,
either by hand (i.e. explicit in code) or by a very carefully controlled
procedure.

If you want to have this for debugging purposes, you might like to check out
the possibilities of Trace[expr, form] first.


--
Hartmut Wolf


  • Prev by Date: RE: Printing Variables and Their Values
  • Next by Date: Maximum Likelihood Problem
  • Previous by thread: RE: Printing Variables and Their Values
  • Next by thread: how to read this data file in mathematica?