Skip to main content
September 15, 2011
Question

How to count columns

  • September 15, 2011
  • 1 reply
  • 952 views

My CF app. start with validating if the text file I'm processing contain the right mount of allowable column.

Each row of my text file should consist of 100 columns, each column is separated by a tab delimiter

How can I count these columns in CF after user uploading the text file. Please help!

This topic has been closed for replies.

1 reply

Inspiring
September 15, 2011

Treat the file as a list delimited by Chr(10),

Treat each list element as a list delimted by Chr(9).

September 20, 2011

Hi Dan, thanks for helping. Here is what I did and did not quite working as expected because the end of list row got included as an empty element in my array

For this test, so that I can see the result on my cfdump, I replaced tab delimiters with bar (|) delimiters.

John|Bates| | |123 test Road|Catonsville |MD|21212 | || | | || |X

Betty|Smith| | |345 My Road|Baltimore|MD|21218 | || | | || |X

Here is what I did: 

 

<cffile action="READ" file="/www/fileLocation/#session.userid#/#FileName#" variable="MyFile">

<CFSET fileContents = #Replace(MyFile,chr(9),"|", "ALL")#>

 

<cfset arr_Rows = #ListToArray(fileContents,crlf,TRUE)#>

<cfdump var="#arr_Rows#"> ------------> I saw extra array elements with blank value after array element X

                                                         So it looks like: 

| John|Bates| | |123 test Road|Catonsville |MD|21212 | || | | || |X  

|------------------------------------------------------------------------------------------

| blank value element

|-------------------------------------------------------------------------------------------

|| Betty|Smith| | |345 My Road|Baltimore|MD|21218 | || | | || |X

|------------------------------------------------------------------------------------------

|-------------------------------------------------------------------------------------------

|| blank value element

|-------------------------------------------------------------------------------------------



I'm looping each of the above element to count the column. (See below)

and when it reach blank element, it throw error because there is no column.



<CFLOOP index="i" array="#arr_Rows#">  </CFLOOP>

 

     <CFIF ArrayLen(ListToArray(i,"|",TRUE)) EQ 14>

        <cfdump var="#ArrayLen(ListToArray(i,"|",TRUE))#> ----------> In here the count and array looks correct                                                        

                                                         -----------------

                                                         John

                                                         ------------------

                                                         Bates

                                                         ------------------

                                                         123 test Rd

                                                         ------------------

                                                         Catonsville

                                                         ------------------

                                                         MD

                                                         ------------------

                                                         21212

                                                         ------------------

                                                         ------------------

                                                         ------------------

                                                         ------------------

                                                         ------------------

                                                         ------------------

                                                         ------------------

                                                         ------------------                                                                                                       

                                                         X

                                                         ------------------                                                 

  <CFELSE>

        <cfdump var="#ArrayLen(ListToArray(i,"|",TRUE))#> -------> this is where it caught the blank element, which is the last element that should not be there.

    </CFIF>

BKBK
Community Expert
Community Expert
October 2, 2011

Use instead <cfset arr_Rows = listToArray(fileContents,crlf,FALSE)>.

I suspect ColdFusion counted crlf as 2 delimiters in a row. The value TRUE instructed it to include an empty field when it encounters 2 delimiters in a row, so it did.