# ToExpression an array

I have a .dat file. I would like to convert a big array of strings(that's what I havein the .dat file) into an array of numbers, for example

a = {{"3,-3,0,5,5,0.000208"}, {"-3,-3,0,5,5,0.000208"},{"3,-3,0,5,5,0.000208"}};


into

b = {{3,-3,0,5,5,0.000208}, {-3,-3,0,5,5,0.000208},{3,-3,0,5,5,0.000208}};


I tried to do

ToExpression[a]


but it does not work.

How can I do it?

Thanks

-
You will likely need to find a function to split strings. –  chuy Mar 24 '14 at 14:14
Can you post the file contents instead? It looks like the file was not imported correctly. This conversion should be done at import time. Try importing as Import[..., "CSV"]. "dat" is not a standard file extension and Mathematica can't guess what's in the file, so you have to tell it. –  Szabolcs Mar 24 '14 at 14:15
@Szabolcs, "a" is a copy paste of the last three elements of the .dat file, I just added the left bracket "{" to give a toy small example with only three elements –  Mencia Mar 24 '14 at 14:18
@Mencia So are there any brackets in the file at all? a is a Mathematica expression. Can you copy and paste the file literally, with newlines preserved, etc.? –  Szabolcs Mar 24 '14 at 14:19
@Szabolcs, thank you so much, Import[..., "CSV"] worked. Yes, the .dat file is a mathematica file, well at least it looks like it... –  Mencia Mar 24 '14 at 14:24

The contents of your file probably look like this:

str =
" 3,-3,0,5,5,0.000208
-3,-3,0,5,5,0.000208
3,-3,0,5,5,0.000208"


In the following I am going to use ImportString to be able to work with str, but the same applies to Import as well.

The values are separated by commas. This format is called a CSV format---Comma Separated Values.

Make sure you always specify the format when using Import to read data. In this case,

 ImportString[str, "CSV"]

{{3, -3, 0, 5, 5, 0.000208}, {-3, -3, 0, 5, 5, 0.000208}, {3, -3, 0, 5, 5, 0.000208}}


You did not mention it in your question (please do next time!) but I think you may have wrote ImportString[str, "Data"] which will default to ImportString[str, "Table"] and results in

 {{"3,-3,0,5,5,0.000208"}, {"-3,-3,0,5,5,0.000208"}, {"3,-3,0,5,5,0.000208"}}


(Which is of course not a correct import of this data.)

Many people will try to use the import format "Data" (when the file has a .dat extension), and you'll find several examples of this online. This is based on a misunderstanding and it is incorrect. Do not do this.

There is no such import format as "Data" in Mathematica. There is however an import element. You'll find this in the documentation:

What happens when you write ImportString[str, "Data"] is that the format defaults to "Table" and the "Data" element is chosen. There's another element for "Table":

ImportString[str, {"Table", "Elements"}]

(* ==> {"Data", "Grid"} *)


You could have use ImportString[str, "Grid"] in the same way, which would also default to "Table" but choose the "Grid" element.

Instead of just specifying the element and letting the format be auto-detected, specify both: ImportString["str, {"CSV", "Grid"}], or let only the element default to "Data" (which is what is needed most of the time).

To sum up:

1. Do specify the correct import format, which in this case is "CSV"

2. ".dat" is not a standard file extension and there no corresponding standard format

3. "Data" does not denote a format in Mathematica, but an import element. Do not use it without specifying a format if you need reliability.

-
a = {{"3,-3,0,5,5,0.000208"}, {"-3,-3,0,5,5,0.000208"}, {"3,-3,0,5,5,0.000208"}};
Join @@ ToExpression@StringSplit[#, ","] & /@ a

-