1

I need to read a file line by line such that it reads the first line, does something with it, then takes the second line, does something with it and so on.

I know how to read a text file line by line:

for(line <- Source.fromFile("file.txt").getLines()) { insert(line) **Use the first line of the file in this function reverse(line) **Use the second line of the file in this function } 

in the insert function, first I want to use the first line of the file, and in the reverse function I want to use the second line, then in the second iteration of the loop, I want to use the 3rd line in the insert function and the 4th line in the reverse function and so on. How to do that?

EDIT: This is just an example. I want a general thing, like suppose if I want to use the first line, second line, third line and then iterate the for loop, how to do that?

3 Answers 3

2

Lots of clever solutions. Here's a simple one using zipWithIndex that handles even cases with an uneven number of lines.

for((line,index) <- Source.fromFile("file.txt").getLines().zipWithIndex) { if (index % 2 == 0) insert(line) else reverse(line) } 
Sign up to request clarification or add additional context in comments.

Comments

1

One more approach, using grouped, which takes into account a (possibly) uneven number of lines,

Source.fromFile("file.txt") .getLines .grouped(2) .map { xs => (xs.head, xs.last.reverse) } 

Note that getLines gives an iterator for fetching one line at a time, sequentially, then grouped gives yet another iterator with paired lines for simultaneous processing. This is in contrast with reading multiple lines of a file at the same time.

3 Comments

I want to make it a general case. Like this was just an example where I wanted first and second line and then loop iteration, suppose I want first, second and third line and then iteration, how to do that?
Increase Int value in grouped, also xs.map{ /*awesome multiple lines processing */}
Note, if the input doesn't have an even number of lines then this gives an exception on "xs.last". It's easy enough to fix up, but messy with n > 2. Also original question had imperative structure this has a more functional structure. That can be fixed by replacing ".map {...}" with ".foreach{ xs => {insert(xs.head), reverse(xs.last)} }". Then again, OP may have been asking for a functional transform. Hard to say.
0

Using sliding to group your lines into pairs of two.

for(pairs <- Source.fromFile("file.txt").getLines().sliding(2, 2)) { insert(pairs.head) reverse(pairs.last) } 

Obviously you'll need to handle the condition where you don't have a list of even length.

2 Comments

I want to make it a general case. Like this was just an example where I wanted first and second line and then loop iteration, suppose I want first, second and third line and then iteration, how to do that?
Figured it out! Thanks all :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.