简体   繁体   English

groovy这个基本的闭包怎么了?

[英]groovy what's wrong with this basic closure?

I've got the following groovy code: 我有以下groovy代码:

dataFile.filterLine() {it =~ /(${searchStr1}|${searchStr2})/ }.each { it ->
    println it
    it.split { list ->
        println "split line:  ${list[0]},  ...."
    }
}

The first println works great -- lists all the matching lines. 第一个println效果很好 - 列出所有匹配的行。 Then, the split causes an error, in fact on the second println. 然后,拆分实际上在第二个println上导致错误。 Obviously it doesn't like the ${list[0]}. 显然它不喜欢$ {list [0]}。 But, I'm not clear what is wrong with that. 但是,我不清楚这有什么问题。

Error is: 错误是:

No signature of method: org.codehaus.groovy.runtime.DefaultGroovyMethods$4.getAt() is applicable for argument types: (java.lang.Integer) values: [0]
Possible solutions: getAt(java.lang.String), putAt(java.lang.String, java.lang.Object), wait(), grep(), getClass(), wait(long). Stacktrace follows:

Thanks 谢谢

filterLine doesn't return what you think it does; filterLine不会返回您的想法; it's a Writable , containing all the matches from the input file. 这是一个Writable ,包含输入文件中的所有匹配项。

Here's a minimal example: 这是一个最小的例子:

f = new File("/home/dave/.bashrc")
w = f.filterLine({ it =~ /alias/ })
println w.class

s = w.toString()
println s

s.eachLine { println "==> ${it}" }

String.split() doesn't take a closure argument. String.split()不接受闭包参数。 I think you might want it.split().each { list -> for the third line. 我认为您可能需要it.split().each { list ->作为第三行。

EDIT: It's actually matching Collection.split(Closure) , where the closure is used to group the contents. 编辑:它实际上匹配Collection.split(Closure) ,其中闭包用于对内容进行分组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM