title: write the results of for loop to csv in groovy
body: <p>currently I am using Groovy to create nested for loops that print the contents of objects to a string intended to be rows of delimited data. I would like to output these strings to a csv file however rather than printing them.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>for (doc in docs) {
      AnnotationSet row = doc.getAnnotations("Final").get("Row")
      AnnotationSet BondCounsel = doc.getAnnotations("Final").get("Bond_Counsel")
      AnnotationSet PurchasePrice = doc.getAnnotations("Final").get("PurchasePrice")
      AnnotationSet DiscountRate = doc.getAnnotations("Final").get("DiscountRate")
      for (b in BondCounsel) {
          for (d in DiscountRate) {
              for (r in row) {
                  for (p in PurchasePrice) {
      println(doc.getFeatures().get("gate.SourceURL") + "|"
      + "mat_amount|" + r.getFeatures().get("MatAmount") + "|"
      + "orig_price|" + p.getFeatures().get("VAL") + "|"
      + "orig_yield|" + r.getFeatures().get("Yield") + "|"
      + "orig_discount_rate|" + d.getFeatures().get("rate")+ "|"
      + "CUSIP|" + r.getFeatures().get("CUSIPVAL1") + r.getFeatures().get("CUSIPVAL2") + r.getFeatures().get("CUSIPVAL3") + "|"
      + "Bond_Counsel|" + b.getFeatures().get("value"))
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>where the output are just a series of strings such as:</p>
  
  <pre><code>filename1|mat_amt|3|orig_price|$230,000.....
  filename2|mat_amt|4|orig_price|$380,000.....
  </code></pre>
  
  <p>I understand I can set up a file writer i.e.</p>
  
  <pre><code>fileWriter = new FileWriter(fileName);
  csvFilePrinter = new CSVPrinter(fileWriter, csvFileFormat);
  csvFilePrinter.printRecord(FILE_HEADER);
  
  &lt;for loop here storing results&gt;
  csvFilePrinter.printRecord(forLoopResults)
  </code></pre>
  
  <p>But I am unsure how to properly format and store what I currently printing in the for loop to be able to pass onto <code>csvFilePrinter.printRecord()</code></p>
  
  <p>Any help would be great.</p>