How to convert an alphanumeric (reference) number containing a decimal point for a string in math

I have a reference number of the following type DAA76647.1, which I want to convert unchanged to a string in Mathematica.

it

myfn [DAA76647.1]

gives as an output

"DAA76647.1"

Is there an easy way to do this? (Input cannot be a string and, except for converting to a string, I do not want to change the input in any other way).

Update

ToString /@ {A1234, 1234.1, A1234 .5} 

gives the following result (where I just entered everything from the keyboard)

 {"A1234", "1234.1", "0.5 A1234"} 

It seems that if something up to the decimal point is alphanumeric, a problem arises.

Possible workaround

Based on the proposed David Carraher solution , the following method is possible:

 ToString[# /.a_ b_ :> ToString[b] <> StringDrop[ToString[a], 1]] & /@ {A1234, 1234.1, A1234 .5} 

as output:

 {"A1234", "1234.1", "A1234.5"} 

This seems to work fine, provided that what comes after the decimal point is not alphanumeric, and provided that what comes earlier does not start from scratch (e.g. 0A123.1).

If alphanumeric characters appear only after the decimal point, this may be included.

 StringReplace[ToString[123.45 B55c], Whitespace -> ""] 

but if alphanumeric processing occurs before and after the decimal point, the number should still be entered as a string.

David Carrach's original proposal

 f[Times[a_, b_]] := ToString[b] <> ToString[a] 
+4
source share
3 answers

If you enter DAA76647DAA76647.1 through an input cell in a Mma laptop, Mma interprets the characters as multiplication. It even automatically inserts a space between 7 and .1 (at least in Mma 8) when you enter it.

 DAA76647DAA76647 .1 // FullForm (*Out= Times[0.1`,DAA76647DAA76647] *) 

This looks promising:

 f[Times[a_, b_]] := ToString[b] <> ToString[a] 

< / "> EDIT : However, as TomD (and I somehow missed), he adds an extra zero to the solution!

 f[Times[DAA76647DAA76647 .1]] (*Out= DAA76647DAA766470.1 *) %//FullForm "DAA76647DAA766470.1" 

TomD later showed how you can handle this with StringDrop ping zero.

This fixed solution will work if only digits are displayed to the right of the decimal point, and if the left side is not interpreted as a product.

If you try to enter DAA76647.01A , Mma will analyze it as

 (*Out= Times[".01",A,DAA76647] *) 

Note that it reorders the components.

I see no way to handle this reordering.

+2
source

The call to myfn[DAA76647.1] should be intercepted during the conversion of input to expression.

You can see that the Login has the form RowBox[{"myfn", "[", RowBox[{"DAA76647", ".1"}], "]"}] :

 In[1]:= myfn[DAA76647 .1] DownValues[InString] Out[1]= myfn[0.1 DAA76647] Out[2]= {HoldPattern[InString[1]] :> ToString[RowBox[{"myfn", "[", RowBox[{"DAA76647", ".1"}], "]"}], InputForm], HoldPattern[InString[2]] :> ToString[RowBox[{"DownValues", "[", "InString", "]"}], InputForm]} 

We could create a special case definition for MakeExpression :

 MakeExpression[RowBox[{"myfn", "[", RowBox[{"DAA76647", ".1"}], "]"}], f_] := MakeExpression[RowBox[{"myfn", "[", "\"DAA76647.1\"", "]"}], f] 

You can see that now myfn[DAA76647 .1] works as desired:

 In[4]:= myfn[DAA76647 .1]//FullForm Out[4]//FullForm= myfn["DAA76647.1"] 

This approach can be generalized to something like

 MakeExpression[RowBox[{"myfn", "[", expr:Except[_String], "]"}], form_] := With[{mexpr = StringJoin[expr /. RowBox -> List]}, Hold[myfn[mexpr]]] myfn[expr_String] := (* what ever you want to do here *) 

Note that the Except[_String] really not needed ... since the following code will not do anything wrong with String.
Currently, the code only works with simple examples with a one-dimensional box structure. If you want something that handles more general input, you can add error checking or additional rules for things like SuperscriptBox and friends. Or hammer it with Evaluate[Alternatives @@ Symbol /@ Names["*Box"]] -> List so that all Box objects become lists and smooth everything.

+3
source

I do not think that you can directly enter this between the brackets of a function call, but

 myfn[InputString[]] 

works for you?

+2
source

Source: https://habr.com/ru/post/1347271/


All Articles