# Removing characters from non-strings

I have a large list of lists that contains mixed data, some real, some strings. The lists are of various lengths and depths. Some of the real data values have an asterisk appended to them, e.g.:

{14.2, 14.7, 15.0, {2.4*, 3.5, 4.4*, {2.9, 10.0}}, ...}


What is an easy way to remove the asterisks?

Since there seems to be some confusion about what I am asking, I'm adding this background. The data is imported from an external file, using Mathematica's Import[] function. In that file, some of the data values have asterisks next to them. The result of the import is a list of lists like I have written above. The data values without an asterisk are interpreted as real, while the values with an asterisk are interpreted as strings. I want to get rid of the asterisks and recover the corresponding data values as real values.

• real data values have an asterisk appended to them how can a number has an asterisk appended to them? You mean it is a string representation of a number? So is it like "4.4*"? Or did you mean 4.4"*" ? but this makes no sense, it is multiplying 4.4 by string "*". How about posting, in Mathematica code, a small example of the list? – Nasser Mar 5 '15 at 2:21

The data values without an asterisk are interpreted as real, while the values with an asterisk are interpreted as strings.

Okay, so the actual format of your imported data is apparently:

dat = {14.2, 14.7, 15.0, {"2.4*", 3.5, "4.4*", {2.9, 10.0}}}


A solution:

fix =
s_String?(StringMatchQ[#, "*\\*"] &) :> InternalStringToDouble @ StringDrop[s, -1];

dat /. fix

{14.2, 14.7, 15., {2.4, 3.5, 4.4, {2.9, 10.}}}

• Well, the quotation marks you put in above do not appear in my data, but I can verify that Mathematica interprets those values as strings by using the Head[] function. Thank you for the solution. – kevinea Mar 8 '15 at 16:38
• @kevinea Right, when I wrote "imported" I meant "after using Import`" as opposed to the original format of your data before Mathematica touched it. Thanks for the Accept. – Mr.Wizard Mar 9 '15 at 0:24