AnsweredAssumed Answered

Using <file:readFile> inside a Loop

Question asked by lipeckham on Aug 10, 2011
Latest reply on Aug 15, 2011 by lipeckham
The purpose of the Gel script I am modifying reads two csv files, and structures the data for inputting into a database table. The old script reads each file in a separate part of the script, essentially duplicating code. I am trying to eliminate duplication and make the script more flexible, both in how many files are read in a session, and to be able to read in files which have the same type of data, but with the data stored in different order.

(That is, file1 may have the data in order: {firstName, lastName, email, userId, phone}, while the second has the order {firstName, lastName, userId, phone, email}.

What I am trying to use is a set of nested loops, the outer loop iterating through the list of files, the next level iterating through the rows of data read from a given file, and then interior loops parsing the row data. In order to parse the data correctly, I need to read the first row of each file, and determine the column header order. My intent was to use the forEach loop indexVar to test for the condition of the loop being at the first row.

I.e. simplified...
<core:forEach items=${fileList.getRows()} var="fileRow" >
   <file:readFile fileName=${fileNameFromList} var="input" />
   <core:forEach items=${input.getRows()} var="dataRow" indexVar=i, begin=0, step=1 >
          <core:when test="${i == '0'}">
               <!-- parse column headers-->
               <!-- parse data rows-->
However, it appears that my assumption, that the "input" variable would be reset for each file:readFile is incorrect. What I am seeing when I run the script is that the rows of the second file are appended to the existing set of rows referenced by the variable input. This means that the inner loop, second time through, is going to iterate through all the data from the first file, before iterating from the second file. It also means it won't pick up differences in the column header order, and will parse the second file's column header row as a regular data row.

At the moment, I see two approaches to this problem, neither of which I have figured out how to implement.

The first, and conceptually the simpler, would be a way to 'reset' the input variable between loops. Any way to do that?

The second approach theoreticallywould be to add a variable "count" outside the loops, initializing it to 0, setting the the "begin" variable to "count", and re-setting "Count" to the value of "i" in between the loops. The test in the <core:when line would have to change to test for "i" being equal "count". Which might work, if I can figure out how to compare two variables. ???


(p.s. This is running the version of gel delivered with Clarity 8.1.)