简体   繁体   中英

groovy what's wrong with this basic closure?

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

String.split() doesn't take a closure argument. I think you might want it.split().each { list -> for the third line.

EDIT: It's actually matching Collection.split(Closure) , where the closure is used to group the contents.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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