hubFS: THE place for F#

. . . are you on The Hub?
Welcome to hubFS: THE place for F# Sign in | Join | Help
in Search

[newb] double array question

Last post 07-01-2008, 10:32 by edst. 2 replies.
Sort Posts: Previous Next
  •  07-01-2008, 8:58 6263

    [newb] double array question

    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!
  •  07-01-2008, 9:18 6264 in reply to 6263

    Re: [newb] double array question

    Either:

    let linesSplit =
        System.IO.File.ReadAllLines(fileName)
        |> Array.map (fun x -> String.split [','] x |> List.map System.Double.Parse)
        |> Array.map List.to_array
    let linesSplit2 =
        System.IO.File.ReadAllLines(fileName)
        |> Array.map (String.split [','])
        |> Array.map (List.map System.Double.Parse)
        |> Array.map List.to_array


    Should work (although I've not tested this!). I believe the first one should be more effient but find the second one easier to read.

    Cheers,
    Rob

    Robert Pickering
    http://strangelights.com
  •  07-01-2008, 10:32 6266 in reply to 6264

    Re: [newb] double array question

    Excellent -- the second one works and makes the most sense to me.

    Thanks!
View as RSS news feed in XML
Powered by Community Server, by Telligent Systems