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

MathGroup Archive 2008

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

Search the Archive

Re: Variable question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92508] Re: Variable question
  • From: Bob F <deepyogurt at gmail.com>
  • Date: Thu, 2 Oct 2008 04:35:12 -0400 (EDT)
  • References: <gc0tkn$9ci$1@smc.vnet.net>

On Oct 1, 4:29 pm, Joh... at giganews.com wrote:
> I have the following test code
>
> c = 3
> var = "c"
> v = StringTake[var, {1}]
> v
>
> v is showing up as c but I want it to show up as 3.  Is there a way to =
do this?
>
> Thanks

Well, you could just assign "3" to the variable c, like

c = "3"
var = c (* this is really redundant, just use the c variable in the
StringTake[ c, {1}] function *)
v = StringTake[var, {1}]

Also, note that the second argument on StringTake is either which
character in the string to extract if you use the list syntax, {1} ,
or if you want the first "n" characters use the interger "n" (without
the quotes of course) - in this case you told it to take the first
character, but if c were longer and you wanted the second character or
the first two characters you could

c = "54321"
v = StringTake[ c, {2}]    (*would give "4" which is the second
character*)
v = StringTake[ c, 2]    (*would give "54" which is the first two
characters*)

Also note that in your original example the last line prints the value
of v a second time since you did not use a ; at the end of the third
line, and without it the value of v is printed once as a result of the
third line and again in the fourth line. So with version 6, the
behaviour of the ; to suppress output is more consistent than version
5 was.

If, what you are trying to do is convert an integer or any expression
to a string you can use the function ToString[expression], e.g.

c = 54321
d = ToString[54321]
v = StringTake[ c, 2]    (*  but c is not a string but an integer so
this will give an error *)
w =  StringTake[ d, 2]    (* would give "54" which is the first two
characters of d *)

See the documentation on ToString[], String Manipulations, String
Operations, etc to get more info...

-Bob



  • Prev by Date: Re: Comparison between Mathematica and other symbolic systems
  • Next by Date: Re: signs in the mathematica output
  • Previous by thread: Re: Variable question
  • Next by thread: Expressing values in the legend in terms of original data