(From the comments ..)
There are better tools for the job: ones specifically designed for parsing delimited files. However, to answer your question, if you must roll-your-own, do not use replace()
. It is simpler to use arrays, specifically listToArray() with includeEmptyFields=true
.
Read in the file, and split the content on CR to create an array of rows:
<!--- split content into an array of rows --->
<cfset rowArray = listToArray(myDataFile, chr(13), true)>
<cfset rowCount = arrayLen(rowArray)>
Then loop through the array, and create a separate array of columns from each row:
<cfloop from="1" to="#rowCount#" index="rowIndex">
<!--- split current row into an array of columns --->
<cfset thisRow = rowArray[ rowIndex ]>
<cfset colArray = listToArray(thisRow , chr(9), true)>
<!--- get first 3 columns (for illustration) --->
<cfset Field1 = colArray[ 1 ]>
<cfset Field2 = colArray[ 2 ]>
<cfset Field3 = colArray[ 3 ])>
...
</cfloop>
If you do not need the loop index for other things, you could also use an array loop instead:
<!--- split content into an array of rows --->
<cfset rowArray = listToArray(myDataFile, chr(13), true)>
<cfloop array="#rowArray#" index="thisRow">
<!--- split current row into an array of columns --->
<cfset columns = listToArray(thisRow, chr(9), true)>
<!--- get first N columns for illustration --->
<cfset Field1 = columns [ 1 ]>
<cfset Field2 = columns [ 2 ]>
<cfset Field3 = columns [ 3 ])>
...
</cfloop>