2017-08-01 76 views
0

爲什麼groovy .each函數只是一次迭代,而它位於另一個迭代中,如下所示代碼?爲什麼groovy每個函數都沒有迭代,而另一個裏面

代碼:

@Grab('com.xlson.groovycsv:groovycsv:1.1') 
import static com.xlson.groovycsv.CsvParser.parseCsv 
import groovy.xml.MarkupBuilder 
import groovy.xml.StreamingMarkupBuilder 
def input = "input.xml" 
def xml = new XmlSlurper().parse(input) 
df = new FileReader("file.csv") 
def writer = new StringWriter() 
def data = parseCsv(df, readFirstLine: true) 
new MarkupBuilder(writer).root { 
    xml.children().each { 
     it.attributes() 
     println ("Here I am") 
     data.eachWithIndex { row,id->      
      println ("This line should be iterated all time") // but iterated only one time    
     } 
    } 
} 

輸出就這樣產生了,

Here I am 
    This line should be iterated all time 
    This line should be iterated all time 
    This line should be iterated all time 
    Here I am 
    Here I am 

預期輸出:

Here I am 
This line should be iterated all time 
This line should be iterated all time 
This line should be iterated all time 
Here I am 
This line should be iterated all time 
This line should be iterated all time 
This line should be iterated all time 
Here I am 
This line should be iterated all time 
This line should be iterated all time 
This line should be iterated all time 

回答

0

這是因爲parseCsv返回Iterator,不是Iterable

source code of groovycsv library

static Iterator parseCsv(Map args = [:], String csv) { 
    new CsvParser().parse(args, csv) 
} 
+0

感謝的人,現在我知道了...... –

相關問題