« Return to Thread: Source.fromFile(file).getLines
How would you tell apart a line that ends in "\n" with one that doesn't? The method getLines doesn't strip away any characters present in the file. Which, by the way, is pretty standard behavior. In Perl, for instance, you need to "chomp" away newlines if you don't want them. Likewise, in Scala, you need to stripLineEnd newlines if you don't want them.--
On Thu, Jul 9, 2009 at 2:07 PM, Ricky Clarkson <ricky.clarkson@...> wrote:
ricky@ricky-desktop:~$ cat test.scala
import scala.io.Source
import java.io.File
object Main {
def main(args: Array[String]) {
for (line <- Source.fromFile(new File("test.scala")).getLines)
print("("+line+")")
}
}
ricky@ricky-desktop:~$ scalac test.scala
ricky@ricky-desktop:~$ scala Main
(import scala.io.Source
)(import java.io.File
)(
)(object Main {
)( def main(args: Array[String]) {
)( for (line <- Source.fromFile(new File("test.scala")).getLines)
)( print("("+line+")")
)( }
)(}
)(
)(
)
Note that I used print instead of println. I can't imagine it's a
good thing to have line contain a line terminator of any kind.
2009/7/9 Daniel Sobral <dcsobral@...>:
> No, not expected. I don't get that either. Try doing map(_ map(_ toInt)) to
> the result of getLines. Maybe you are getting "\r\n" and confusing it with
> "\n\n".
>
> On Thu, Jul 9, 2009 at 1:38 PM, Gilles SCOUVART <Gilles.SCOUVART@...>
> wrote:
>>
>> Hello all,
>>
>> I was surprised to notice that using Source.fromFile(file).getLines on a
>> file with empty lines, those were added to the previous non empty line.
>> I.e. if the file contains this:
>> ------
>> test
>>
>>
>> ------
>> then getLines.next would return "test\n\n" (written java-echap-style).
>> I expected it would iterate on three lines instead: "test", "", and "".
>> This is a little bit annoying for pattern matching on full lines...
>>
>> Is this expected behaviour?
>>
>> Gilles Scouvart.
>
>
> --
> Daniel C. Sobral
>
> Something I learned in academia: there are three kinds of academic reviews:
> review by name, review by reference and review by value.
>--
Ricky Clarkson
Java Programmer, AD Holdings
+44 1565 770804
Skype: ricky_clarkson
Google Talk: ricky.clarkson@...
Daniel C. Sobral
Something I learned in academia: there are three kinds of academic reviews: review by name, review by reference and review by value.
« Return to Thread: Source.fromFile(file).getLines
| Free embeddable forum powered by Nabble | Forum Help |