title: Reading CSV into Map[String, Array[String]] in Scala
body: <p>Given a csv in the format below, what is the best way to load it into Scala as type <code>Map[String, Array[String]]</code>, with the first key being the unique values for <code>Col2</code>, and the value <code>Array[String]]</code> as all co-occurring values of Col1?</p>
  
  <pre><code>a,1,
  b,2,m
  c,2,
  d,1,
  e,3,m
  f,4,
  g,2,
  h,3,
  I,1,
  j,2,n
  k,2,n
  l,1,
  m,5,
  n,2,
  </code></pre>
  
  <p>I have tried to use the function below, but am getting errors trying to add to the Option type:
  <code>+= is not a member of Option[Array[String]]</code></p>
  
  <p>In addition, I get <code>overloaded method value ++ with alternatives:</code>
  with regards to the line <code>case None =&gt; mapping ++ (linesplit(2) -&gt; Array(linesplit(1)))</code></p>
  
  <pre><code>def parseCSV() : Map[String, Array[String]] = {
      var mapping = Map[String, Array[String]]()
      val lines = Source.fromFile("test.csv")
      for (line &lt;- lines.getLines) {
        val linesplit = line.split(",")
        mapping.get(linesplit(2)) match {
          case Some(_) =&gt; mapping.get(linesplit(2)) += linesplit(1)
          case None =&gt; mapping ++ (linesplit(2) -&gt; Array(linesplit(1)))
        }
      }
      mapping
    }
  }
  </code></pre>
  
  <p>I am hoping for a <code>Map[String, Array[String]]</code> like the following:</p>
  
  <pre><code>(2 -&gt; Array["b","c","g","j", "k", "n"])
  (3 -&gt; Array["e","h"])
  (4 -&gt; Array["f"])
  (5 -&gt; Array["m"])
  </code></pre>