views:

47

answers:

2

This is a follow up to the question asked here: http://stackoverflow.com/questions/3360191/groovy-parsing-text-file

The difference now is my file has a header, I attempted to read past the header first and then onto the content that I am wanting, but for some reason it doesn't seem to cooperate.

def dataList = []
def theInfoName = 'testdata.txt'
boolean headersDone = false    //header set to false by default

File theInfoFile = new File( theInfoName )

if( !theInfoFile.exists() ) {
  println "File does not exist"
} else {
  def driveInfo = [:]
  // Step through each line in the file
  theInfoFile.eachLine { line ->

  //this is where im trying to account for the header
  if(!headersDone) {      //look if line contains "..." if it does that turn headersDone to true
   if(line.contains("...")) {
     headersDone = true
   }
  } else {
     // If the line isn't blank
     if( line.trim() ) {
       // Split into a key and value
       def (key,value) = line.split( '\t: ' ).collect { it.trim() }
       // and store them in the driveInfo Map
       driveInfo."$key" = value
     }
     else {
       // If the line is blank, and we have some info
       if( driveInfo ) {
         // store it in the list
         dataList << driveInfo
         // and clear it
        driveInfo = [:]
       }
     }
  }
  // when we've finished the file, store any remaining data
  if( driveInfo ) {
    dataList << driveInfo
  }
}

dataList.eachWithIndex { it, index ->
  println "Drive $index"
  it.each { k, v ->
    println "\t$k = $v"
  }
}

I tried it out with the code provided in the previous post to make sure it wasn't something I was doing differently and it came with the same output.

What happens is that it posts the same block of information 11 times.

The header looks is the following:

Random date information here with some other info
Slightly more random information followed by

Examining hard disk information ...

HDD Device 0 : /dev/sda
HDD Model ID  : ST3160815A
HDD Serial No : 5RA020QY
HDD Revision  : 3.AAA
HDD Size     : 152628 MB
Interface    : IDE/ATA
Temperature         : 33 C
Health  : 100%
Performance  : 70%
Power on Time : 27 days, 13 hours
Est. Lifetime : more than 1000 days

HDD Device 1 : /dev/sdb
HDD Model ID  : TOSHIBA MK1237GSX
HDD Serial No : 97LVF9MHS
HDD Revision  : DL130M
HDD Size     : 114473 MB
Interface    : S-ATA
Temperature  : 30 C
Health  : 100%
Performance  : 100%
Power on Time : 38 days, 11 hours
Est. Lifetime : more than 1000 days

Does anyone know why it is printing out duplicate of the information?

A: 

Can't see anything obviously wrong with the code. I suggest to add a couple of printlns so you can see how the maps, lists and variables change. That might give you an idea where the bug might be.

Aaron Digulla
The first thing I did was set println's to see if my boolean headersDone changes, which is does. I'll have to check the maps and lists in the morning. I'm out for the night, will check responses and will post back with more information once I have checked how the maps and lists change. I'm thinking that it's not omitting the header at all.
JohnStamos
Thank you for your time Aaron, going through my code line by line (retyping it) ended up fixing the issue. Looking back on it, it would almost have to be a problem with something being left inside the loop. (hence duplicate data)
JohnStamos
+1  A: 

The problem is the addition of the "last" driveInfo to the dataList:

// when we've finished the file, store any remaining data
if( driveInfo ) {
   dataList << driveInfo
}

It has to be one curly brace below its current position, otherwise it belongs to the eachLine closure.

Christoph Metzendorf
I didn't exactly locate the problem, but I'm pretty sure that this was the issue. Thank you for the response Chris.
JohnStamos