Message Boards Message Boards

0
|
1731 Views
|
6 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Unexpected result: converting text started by "1" to expression with ToExpression

Why when converting a text string started with "1" to an expression with ToExpression, the output is an expression without the "1"?

Example:

In[1406]:= {ToExpression["Wc1F1C1"], ToExpression["1Wc1F1C1"], 
 ToExpression["2Wc1F1C1"]}

Out[1406]= {Wc1F1C1, Wc1F1C1, 2 Wc1F1C1}

If instead of "1" it is another number, the same does not happen.
Thank you.

6 Replies

It is basically the same reason why

ToExpression["1+1"]

gives 2, and not 1+1. Here is a workaround,if you really need it:

Block[{Times = Inactive[Times]},
 ToExpression["1Wc1F1C1"]]
POSTED BY: Gianluca Gorni

Ok, But the last "1" of the string "Wc1F1C1" does not multiply it: it remains after applying ToExpression.

In[1425]:= {ToExpression["Wc1F1C1"], ToExpression["1Wc1F1C1"], 
 ToExpression["2Wc1F1C1"], 2 ToExpression["2Wc1F1C1"], 
 ToExpression["2Wc1F1C1"] 2, ToExpression["2Wc1F1C1"]*2}

Out[1425]= {Wc1F1C1, Wc1F1C1, 2 Wc1F1C1, 4 Wc1F1C1, 4 Wc1F1C1, 
 4 Wc1F1C1}

Thanks.

The point is that a name for a symbol cannot start with a digit. This is described in the documentation for "Symbol" / "Details":

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.

You can also see that effect when trying to input a symbol name e.g. as 5xxx. The 5 immediately becomes a multiplier.

POSTED BY: Henrik Schachner

Thank you, very much.

the one is part of the name

In[7]:= a1

Out[7]= a1

POSTED BY: Frank Kampas

1 multiplied by an expression is just the expression

In[2]:= 1 a

Out[2]= a

POSTED BY: Frank Kampas
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract