1
$\begingroup$

I'm trying to label my symbolic object's using Symbol

Works as expected

Symbol["M" <> ToString[5]]

Output

M5

When I try do two variable indexing I get a result that doesn't work

Symbol[StringJoin["M", ToString[5], "_", ToString[12]]]
Symbol["M" <> ToString[5] <> "_" <> ToString[12]]

Output

Symbol["M5_12"]
Symbol["M5_12"]

So, the input to all three cases appears to be strings:

Head @ ("M" <> ToString[5])
Head @ (StringJoin["M", ToString[5], "_", ToString[12]])
Head @ ("M" <> ToString[5] <> "_" <> ToString[12])

Output

String      
String      
String      

I'm confused.

Why does Symbol appear to have different output for the same class of input?

$\endgroup$

closed as off-topic by Michael E2, Jason B., Kuba Nov 26 '18 at 6:14

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Michael E2, Jason B., Kuba
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ Well, darn, ain't it hard to find an explicit definition of a valid symbol name. The documentation for Symbol may be as close as one can get: "The string "name" in Symbol["name"] must be an appropriate name for a symbol. It can contain any letters, letter-like forms, or digits, but cannot start with a digit." The clue is "letter-like form"; and an underscore is not a letter-like form but stands for Blank[]. Other syntactic marks (+, :, =, etc.) are also not letter-like forms. $\endgroup$ – Michael E2 Nov 26 '18 at 1:39
  • $\begingroup$ Better user experience would trigger an explicit error. $\endgroup$ – Mikhail Nov 26 '18 at 1:42
  • 1
    $\begingroup$ I get an error when I execute Symbol["M5_12"]: i.stack.imgur.com/rPj7i.png $\endgroup$ – Michael E2 Nov 26 '18 at 1:43
  • 1
    $\begingroup$ The error message from this input very clearly states that that this is not a valid symbol name. I do not understand what your confusion is. $\endgroup$ – Jason B. Nov 26 '18 at 2:18
  • $\begingroup$ I don't see that error. Pehraps my version is different. I'll check when I get home. $\endgroup$ – Mikhail Nov 26 '18 at 2:26
2
$\begingroup$

Looks like the variable names were invalid on the case of the underscore:

For example, when I change the naming function to the following I get the expected output:

Symbol[StringJoin["M", ToString[5], "x", ToString[12]]]
$\endgroup$
  • $\begingroup$ _ or Blank cannot appear in a variable name since it is a pattern object that can stand for any Wolfram Language expression. $\endgroup$ – Bob Hanlon Nov 26 '18 at 1:45

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