Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: writing a function that would print the name of a

  • To: mathgroup at smc.vnet.net
  • Subject: [mg97673] Re: [mg97609] writing a function that would print the name of a
  • From: Bob Hanlon <hanlonr at cox.net>
  • Date: Wed, 18 Mar 2009 04:55:47 -0500 (EST)
  • Reply-to: hanlonr at cox.net

f1[x_String] :=
  "The variable " <> x <> " contains the value " <>
   ToString[ToExpression[x]]

x = 10

10

f1["x"]

The variable x contains the value 10

SetAttributes[f2, HoldFirst]

f2[x_Symbol] :=
  "The variable " <> ToString[HoldForm[x]] <> " contains the value " <>
   ToString[x]

f2[x]

The variable x contains the value 10



Bob Hanlon


On Tue, Mar 17, 2009 at 8:49 AM , johnclin at hss.caltech.edu wrote:

> I have a question that seems simple but I just could not figure out
> how to do it.
>
> I am trying to write a mathematica function that will print the *name*
> of the variable that I pass to the function as well as the value
> itself.
>
> For example, let's say that I define a variable x = 10.
>
> Now, I want to write a function that will print:
> "The variable x contains the value 10".
>
> I tried something like:
> displayOutput [varToPrint_] := (
> Print["The variable "];
> Print[ToString[varToPrint]]; (* <-- this is the problematic line*)
> Print["contains the value "];
> Print[x];
> )
>
> BTW, I do realize that the following works:
> x = 10;
> Print["The symbol ", HoldForm[x], " has a value of ", x];
>
> So there is something about how Mathematica evaluates expression
> inside a function that I am not understanding. Any suggestions?


  • Prev by Date: Re: Problem with mathlink:PLEASE HELP ME
  • Next by Date: Filling Between Surfaces
  • Previous by thread: Re: Wolfram WorkBench and iDisk
  • Next by thread: Filling Between Surfaces