Groovy这个基本封闭有什么问题? [英] groovy what's wrong with this basic closure?

查看:319
本文介绍了Groovy这个基本封闭有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

第一个println工作得很好 - 列出所有匹配的行。然后,分割会导致错误,实际上在第二个println上。显然它不喜欢$ {list [0]}。但是,我不清楚这有什么问题。



错误是:

 方法没有签名:org.codehaus.groovy.runtime.DefaultGroovyMethods $ 4.getAt()适用于参数类型:(java.lang.Integer)values:[0] 
可能的解决方案:getAt (java.lang.String),putAt(java.lang.String,java.lang.Object),wait(),grep(),getClass(),wait(long)。 Stacktrace如下:

谢谢

解决方案

filterLine 不会返回您认为它的作用;它是一个 Writable ,包含输入文件中的所有匹配项。



这是一个简单的例子:

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

s = w.toString()
println s

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


I've got the following groovy code:

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. Then, the split causes an error, in fact on the second println. Obviously it doesn't like the ${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; it's a Writable, containing all the matches from the input file.

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}" }

这篇关于Groovy这个基本封闭有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆