如何在Groovy中获取子列表值

如何在Groovy中获取子列表值,groovy,Groovy,我有下面的方法,我试图比较两个文本文件 所以基本上我需要比较sourceFile行的substring和generatedFile行。 在这里,我正在努力获取子列表的价值。因为我想在得到值后再做子字符串 def FileCompare(String sourceFile, String generatedFile) { def firstList = new File(sourceFile).readLines() def m = 0 def n = 1 new Fil

我有下面的方法,我试图比较两个文本文件

所以基本上我需要比较
sourceFile
行的
substring
generatedFile
行。 在这里,我正在努力获取子列表的价值。因为我想在得到值后再做子字符串

def FileCompare(String sourceFile, String generatedFile) {
   def firstList = new File(sourceFile).readLines()
   def m = 0
   def n = 1

   new File(generatedFile).readLines().each {
      println  firstList.subList(m,n)
      println  it.substring(0,8)
      m = m + 1
      n = n + 1

      // more coding to be done for compare  
   }
}
上面的
println firstList.subList(m,n)
我可以看到结果为
[2104808660 COMPANYX]

但目的是
子字符串

源文件

2011070620110707             COMPANYX            
2104808660                   COMPANYX
2104808662                   COMPANYX
2114454303                   COMPANYX
2114454303                   COMPANYX
00000004
2011070620110707             COMPANYX            
2104808665                   COMPANYX
2104808661                   COMPANYX
2114454301                   COMPANYX
2114454301                   COMPANYX
00000005
生成的文件

2011070620110707             COMPANYX            
2104808660                   COMPANYX
2104808662                   COMPANYX
2114454303                   COMPANYX
2114454303                   COMPANYX
00000004
2011070620110707             COMPANYX            
2104808665                   COMPANYX
2104808661                   COMPANYX
2114454301                   COMPANYX
2114454301                   COMPANYX
00000005

那么您想从两个文件中提取每行的前8个字符吗?请看一个例子:

def firstList = new File(sourceFile).readLines()

new File(generatedFile).readLines().eachWithIndex { line, index ->

    println  firstList[index].substring(0,8)
    println  line.substring(0,8)

    // ...or even shorter with ranges:

    println  firstList[index][0..7]
    println  line[0..7]

    // more coding to be done for compare  
}

那么您想从两个文件中提取每行的前8个字符吗?请看一个例子:

def firstList = new File(sourceFile).readLines()

new File(generatedFile).readLines().eachWithIndex { line, index ->

    println  firstList[index].substring(0,8)
    println  line.substring(0,8)

    // ...or even shorter with ranges:

    println  firstList[index][0..7]
    println  line[0..7]

    // more coding to be done for compare  
}

您可以将两个文件的内容读取到两个列表中,并比较列表:

def toList(filename) {
   new File(sourceFile).readLines().collect { it[0..7] }
}

def sourceList = toList(sourceFile)
def generatedList = toList(generatedFile)

//comparison

您可以将两个文件的内容读取到两个列表中,并比较列表:

def toList(filename) {
   new File(sourceFile).readLines().collect { it[0..7] }
}

def sourceList = toList(sourceFile)
def generatedList = toList(generatedFile)

//comparison

如果不需要为其他任务保存文件行,则可以尝试此方法

[new File(sourceFile).readLines(), new File(generatedFile).readLines()]
    .transpose().each{ line ->

    println line[0][0..7]  //Line from sourceFile
    println line[1][0..7]  //Line from generatedFile

    // compare code here.
}

如果不需要为其他任务保存文件行,则可以尝试此方法

[new File(sourceFile).readLines(), new File(generatedFile).readLines()]
    .transpose().each{ line ->

    println line[0][0..7]  //Line from sourceFile
    println line[1][0..7]  //Line from generatedFile

    // compare code here.
}

请说明输入文件中的数据,并澄清预期的输出。@MichaelEaster补充到问题中。如图所示,
2104808660
2104808665
不符合要求。(第二行)因此我想检查整个字符串的那部分(这是固定长度),并在自动化测试中得到失败断言。m=n+1的子列表[n,m]与firstList[n]相同。然后调用你的子字符串,你应该完成吗?请说明输入文件中的数据,并澄清预期的输出。@MichaelEaster补充到问题中。如图所示,
2104808660
2104808665
不符合要求。(第二行)因此我想检查整个字符串的那部分(这是固定长度),并在自动化测试中得到失败断言。m=n+1的子列表[n,m]与firstList[n]相同。然后调用你的子字符串,你应该完成吗?