I am having this error :
error: illegal start of simple expression def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield (val a = line.split(",")) (a[0],a[4].toDouble)
for this line of code :
def process_alcs(lines: List[String]) : List[(String, Double)] =
for (line <- lines) yield (val a = line.split(",")) (a[0],a[4].toDouble)
I first thought it was a parenthese mismatch but I checked 100 times
Try
def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield {
val a = line.split(",")
(a(0),a(4).toDouble)
}
Basically, you are performing a multi line evaluation in your yield. As such, you must encapsulate the code in it with {
instead of (
. Also, both statements should be encapsulated in a single {}
pair since they build up to what you want to yield.
Additionally, array members in scala are accessed using ()
and not []
. So, you want to use a(0) instead of a[0]
On a separate note, this seems related to another question someone else asked today in which case he wanted to convert to double the value in the 4th column. To access the 4th column, you would want to use a(3).
In that case, the code becomes,
def process_alcs(lines: List[String]) : List[(String, Double)] = for (line <- lines) yield {
val a = line.split(",")
(a(0), a(3).toDouble)
}
Hope this helps. Let me know if you have any further questions :)
Edit: To convert to map, try,
def process_as_map(lines: List[String]) : Map[String, Double] = {
val results: List[(String, Double)] = for (line <- lines) yield {
val a = line.split(",")
(a(0), a(3).toDouble)
}
results.toMap
}
Refer comments for explanation, on why I tweaked as such based on your code
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments