Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Converting a string to a symbol' topicStudent Support Forum > General > Archives > "Converting a string to a symbol"

Next Comment >Help | Reply To Topic
Author Comment/Response
Hari Sundararajan
10/31/06 8:54pm

Hello

I am given an expression like this:
x[la]y[ub] + x[lqw]y[ubf]

And the result must be this:
x_a y^b + x_qw y^bf

In other words, every time a pattern of [l.*] is seen, the .* must become the subscript, and a pattern of [u.*] is seen the .* must become the superscript.

My initial reaction to this was as follows:

/. {x_[p_] /; StringMatchQ[
ToString[p], RegularExpression["u.*"]] -> Superscript[x,StringDrop[ToString[p],{1}]}

In other words, parse it, if you see any string of the form x[p], check if p is of the form l... or u... If so make whatever comes after the 'l' or 'u' the subscript or superscript of x.

But in the second case, ToString[p] returns simply p instead of using the value of p. Why? In the first instance of ToString[p] (when checking with the regular expression) it correctly uses the value of p, but in the second case it uses p itself.

URL: ,

Subject (listing for 'Converting a string to a symbol')
Author Date Posted
Converting a string to a symbol Hari Sundara... 10/31/06 8:54pm
Re: Converting a string to a symbol yehuda ben-s... 10/31/06 11:54pm
Re: Converting a string to a symbol Hari Sundara... 11/02/06 8:33pm
Next Comment >Help | Reply To Topic