0

I am very naive in programming as well as in scala. I have a file with following content:

football type game
John comment "football is the best game"

I want to make it tuple like this

(football, type, game)
(John, comment, "football is the best game")

Could somebody tell me how can I do that in scala.

5
  • 4
    What have you tried? Show your code attempts and we can help correct any misconceptions. Commented Jan 23, 2017 at 5:16
  • 1
    fileLines.map { line => { val lineSplit = line.split(" ") (lineSplit(0), lineSplit(1), lineSplit(2)) } } But the problem with 3rd field of tuple. Commented Jan 23, 2017 at 5:20
  • Instead of just using the third element from the array, just concatenate everything after the first two elements, and use that as the third field of the tuple Commented Jan 23, 2017 at 6:01
  • So, does this file have more lines besides these 2? Can we have quoted elements anywhere or just in the third position? Can there be quotes inside quotes? Commented Jan 23, 2017 at 6:12
  • This file has thousands of lines but the structure is similar. Quoted elements are in the third position only. Commented Jan 23, 2017 at 6:21

1 Answer 1

3

Your question is a bit vague. If the multi-word string is limited to 3rd element of the tuple, it can be accomplished like so:

fileLines.map { line =>
  val lineSplit = line.split(" ")
  (lineSplit(0), lineSplit(1), lineSplit.drop(2).mkString(" "))
}

But if the quote-delimited string can happen for any of the three tuple members then you've got a lot more processing involved. I think I'd be tempted to split the entire string and then do a search for any elements with a single quote mark and then try to match them. Not trivial.

Sign up to request clarification or add additional context in comments.

Comments

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.