6

I'm trying to read a csv file and return it as an array of arrays of doubles (Array[Array[Double]]). It's pretty clear how to read in a file line by line and immediately print it out, but not how to store it in a two-dimensional array.

def readCSV() : Array[Array[Double]] = {
    val bufferedSource = io.Source.fromFile("/testData.csv")
    var matrix :Array[Array[Double]] = null
    for (line <- bufferedSource.getLines) {
        val cols = line.split(",").map(_.trim)
        matrix = matrix :+ cols
    }
    bufferedSource.close
    return matrix
}

Had some type issues and then realized I'm not doing what I thought I was doing. Any help pointing me on the right track would be very appreciated.

2 Answers 2

6

It seems your question was already answered. So just as a side note, you could write your code in a more scalaish way like this:

def readCSV() : Array[Array[Double]] = {
  io.Source.fromFile("/testData.csv")
    .getLines()
    .map(_.split(",").map(_.trim.toDouble))
    .toArray
}
Sign up to request clarification or add additional context in comments.

Comments

1

1) You should start with an empty Array unstead of null. 2) You append items to an Array with the operator :+ 3) As your result type is Array[Array[Double]] you need to convert the strings of the csv to Double.

def readCSV() : Array[Array[Double]] = {
    val bufferedSource = io.Source.fromFile("/testData.csv")
    var matrix :Array[Array[Double]] = Array.empty
    for (line <- bufferedSource.getLines) {
        val cols = line.split(",").map(_.trim.toDouble)
        matrix = matrix :+ cols
    }
    bufferedSource.close
    return matrix
}

1 Comment

Thank you! Turns out this gets you the array of rows, not the array of columns, but it doesn't take much to turn that into the columns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.