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

编程入门 行业动态 更新时间:2024-10-24 04:47:34
在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)

更多推荐

本文发布于:2023-08-07 20:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   逐行   scala   reading   line

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!