I've been trying to get up to speed on F# by converting a little prototype data analysis script. I have versions of the script in Java, Groovy and Python among others. I learn mostly by example but haven't found much help in the existing F# tutorials and such -- these seem to deal much more with data structures and the language rather than doing what I would deem typical work. I guess I'm mostly struggling with the whole "functional" concepts as well.
Anyway, I have a CSV file containing double values. I have found a few different code snippets that can parse the file as strings in various manners, but I haven't yet figured out how to convert to a double array. I've tried to use System.Convert.ToDouble but get either overload issues or runtime exceptions because String can't be cast to IConvertable.
What's the best way to cast a string to a double? Or more specifically a string array to a 2D double array?
Here's a little block that reads my file OK:
let linesSplit =
System.IO.File.ReadAllLines(fileName)
|> Array.map (String.split [','])
|> Array.map List.to_array
printfn "%a" output_any linesSplit
And this gives me what I expect (strings). How can I get this into a double array. I've tried different variations of
data.[i,j] <- System.Convert.ToDouble( ... ) without success.
For the input data file that looks like:
-0.056379,-0.082208
-0.085795,-0.087651
-0.096692,-0.084949
-0.065759,-0.084962
-0.025618,-0.084975
-0.039906,-0.079558
-0.075863,-0.076856
-0.014209,-0.076869
I get this as output:
[|[|"-0.056379"; "-0.082208"|]; [|"-0.085795"; "-0.087651"|];
[|"-0.096692"; "-0.084949"|]; [|"-0.065759"; "-0.084962"|];
[|"-0.025618"; "-0.084975"|]; [|"-0.039906"; "-0.079558"|];
[|"-0.075863"; "-0.076856"|]; [|"-0.014209"; "-0.076869"|]|]Thinks for helping yet another F# newb!