在scala中按时(逐行)读取2个文件(reading 2 files at time (line by line) in scala)

我是scala的新手,我遇到了这个问题:

def main(args: Array[String]){ val source = Source.fromFile(args(0)) val target = Source.fromFile(args(1)) for (lines <- source.getLines ; linet <- target.getLines) { println(lines + " =source| target= " + linet); } }

如果源文件和目标文件包含普通数字:1,2(每行一个数字),结果为:

1 =source| target= 1 1 =source| target= 2

但是,我希望:

1 =source| target= 1 2 =source| target= 2

问题:第二个文件(目标)被正确读取(逐行,即1和2),在第一个(源)中,只读取第一行(即1)。

最可能的问题在于for循环。 我虽然运营商“;” 表现得像“&&”,因此应该从两个文件中读取一行。 我试过替换“;” 通过“&&”但它没有用。

任何线索将深深感激! 托马斯

I'm new to scala and I hit this problem:

def main(args: Array[String]){ val source = Source.fromFile(args(0)) val target = Source.fromFile(args(1)) for (lines <- source.getLines ; linet <- target.getLines) { println(lines + " =source| target= " + linet); } }

If source and target file contain plain numbers: 1, 2 (one number on each line), the results is:

1 =source| target= 1 1 =source| target= 2

However, I'd expect:

1 =source| target= 1 2 =source| target= 2

Problem: the second file (target) is read correctly (line by line, i.e. 1 and 2), in the first one (source), only the first line (i.e. 1) is read.

Most likely problem lies in for-loop. I though operator ";" behaves like "&&" so one line should be read from both files at time. I tried replaced ";" by "&&" but it didn't work.

any clue will be deeply appreciated! Tomas

最满意答案

您可以压缩它们并迭代线对:

def main(args: Array[String]){ val source = Source.fromFile(args(0)).getLines val target = Source.fromFile(args(1)).getLines for ((s,t) <- source.zip(target)) { println(s + " =source| target= " + t); } }

你的方法的问题是,为(x < - xs,y < - ys)编写的代码产生笛卡尔积。 在你的情况下,只要遍历第一个迭代器,它就会停止产生产品项目(请记住 - 迭代器只能遍历一次)。

UPDATE。

你的for循环在java / C ++ / ...中是模拟的:

for(int i = 0; i < source.length; i++) for(int j = 0; j < target.length; j++) { String s = source[i]; String t = target[j]; // println .... }

(除了那个事实,上面我还没有使用过迭代器)

You can zip them and iterate through line pairs:

def main(args: Array[String]){ val source = Source.fromFile(args(0)).getLines val target = Source.fromFile(args(1)).getLines for ((s,t) <- source.zip(target)) { println(s + " =source| target= " + t); } }

The problem with your approach is that the code written like for(x <- xs, y <- ys) produces cartesian product. In your case it stops to yield items of product as far as first iterator is traversed (bear in mind -- iterators traversable only once).

UPDATE.

Your for loop is analog for this in java/C++/...:

for(int i = 0; i < source.length; i++) for(int j = 0; j < target.length; j++) { String s = source[i]; String t = target[j]; // println .... }

(Besides that fact, that above I haven't used iterators)

在scala中按时(逐行)读取2个文件(reading 2 files at time (line by line) in scala)

我是scala的新手,我遇到了这个问题:

def main(args: Array[String]){ val source = Source.fromFile(args(0)) val target = Source.fromFile(args(1)) for (lines <- source.getLines ; linet <- target.getLines) { println(lines + " =source| target= " + linet); } }

如果源文件和目标文件包含普通数字:1,2(每行一个数字),结果为:

1 =source| target= 1 1 =source| target= 2

但是,我希望:

1 =source| target= 1 2 =source| target= 2

问题:第二个文件(目标)被正确读取(逐行,即1和2),在第一个(源)中,只读取第一行(即1)。

最可能的问题在于for循环。 我虽然运营商“;” 表现得像“&&”,因此应该从两个文件中读取一行。 我试过替换“;” 通过“&&”但它没有用。

任何线索将深深感激! 托马斯

I'm new to scala and I hit this problem:

def main(args: Array[String]){ val source = Source.fromFile(args(0)) val target = Source.fromFile(args(1)) for (lines <- source.getLines ; linet <- target.getLines) { println(lines + " =source| target= " + linet); } }

If source and target file contain plain numbers: 1, 2 (one number on each line), the results is:

1 =source| target= 1 1 =source| target= 2

However, I'd expect:

1 =source| target= 1 2 =source| target= 2

Problem: the second file (target) is read correctly (line by line, i.e. 1 and 2), in the first one (source), only the first line (i.e. 1) is read.

Most likely problem lies in for-loop. I though operator ";" behaves like "&&" so one line should be read from both files at time. I tried replaced ";" by "&&" but it didn't work.

any clue will be deeply appreciated! Tomas

最满意答案

您可以压缩它们并迭代线对:

def main(args: Array[String]){ val source = Source.fromFile(args(0)).getLines val target = Source.fromFile(args(1)).getLines for ((s,t) <- source.zip(target)) { println(s + " =source| target= " + t); } }

你的方法的问题是,为(x < - xs,y < - ys)编写的代码产生笛卡尔积。 在你的情况下,只要遍历第一个迭代器,它就会停止产生产品项目(请记住 - 迭代器只能遍历一次)。

UPDATE。

你的for循环在java / C ++ / ...中是模拟的:

for(int i = 0; i < source.length; i++) for(int j = 0; j < target.length; j++) { String s = source[i]; String t = target[j]; // println .... }

(除了那个事实,上面我还没有使用过迭代器)

You can zip them and iterate through line pairs:

def main(args: Array[String]){ val source = Source.fromFile(args(0)).getLines val target = Source.fromFile(args(1)).getLines for ((s,t) <- source.zip(target)) { println(s + " =source| target= " + t); } }

The problem with your approach is that the code written like for(x <- xs, y <- ys) produces cartesian product. In your case it stops to yield items of product as far as first iterator is traversed (bear in mind -- iterators traversable only once).

UPDATE.

Your for loop is analog for this in java/C++/...:

for(int i = 0; i < source.length; i++) for(int j = 0; j < target.length; j++) { String s = source[i]; String t = target[j]; // println .... }

(Besides that fact, that above I haven't used iterators)