lets say I want to convert this

Subscript[PSternMat, 2, 13]

into this


I have written this rule

Subscript[PSternMat, 2, 13] /. {Subscript[n_, a_, b_] :> 
ToExpression[ToString[n] <> "$" <> ToString[a] <> "$" <> ToString[b]]}

which works fine.

But now I want to convert it back into it's original form. How can I do that


  • $\begingroup$ a quick solution might be something like Apply[Subscript, Through[{First, Sequence @@ Rest[#] &}[ Map[ToExpression, StringSplit[ToString[HoldForm[yourvar$123$12]], "$"]]]]] but I don't know how robust it might be generally speaking $\endgroup$ – user42582 Nov 1 '17 at 14:56
  • 1
    $\begingroup$ This seems like more trouble than it's worth, just because you want to use subscripts. $\endgroup$ – J. M.'s ennui Nov 2 '17 at 6:03
c = PSternMat$2$13
Subscript@@ToExpression@StringSplit[ToString@c, "$"]
  • $\begingroup$ I don't think your answer does what the OP asks.. The 1st argument to the Subscript form should be a symbol, and the remaining two should be integers. $\endgroup$ – m_goldberg Nov 2 '17 at 4:25
  • $\begingroup$ i didn't think about it, i checked the result and the structure with TreeForm and they were equal, i didn't use SameQ, but now i think i fixed the error $\endgroup$ – Alucard Nov 2 '17 at 5:08
  • $\begingroup$ This does work, but only in this specific case. If for instance the variable is squared and I want to convert it gives the wrong answer. $\endgroup$ – OhmSweetOhm Nov 2 '17 at 15:54
  • $\begingroup$ Can pattern matching be used for the task? $\endgroup$ – OhmSweetOhm Nov 2 '17 at 15:54
  • 1
    $\begingroup$ Maybe something using this: StringCases[ToString[PSternMat$2$13], n__ ~~ "$" ~~ a__ ~~ "$" ~~ b__] $\endgroup$ – OhmSweetOhm Nov 2 '17 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.