CSV for multidimensional array with ColdFusion

I am trying to load a CSV file into an array using ColdFusion (version 7.0.2.142559). Now I get the following error:

A scalar value of type coldfusion.runtime.Struct cannot be assigned to a two-dimensional ColdFusion array. A two-dimensional ColdFusion array can contain only one-dimensional ColdFusion arrays and Java List objects.

My CSV file is configured in this format:

a,b c,d e,f 

This is my first exit with ColdFusion, so I probably have a simple syntax error that I just can't see. The code is below.

 <!--- get the current full path of the current ---> <cfset currentPath = getCurrentTemplatePath()> <cfset currentDirectory = getDirectoryFromPath(currentPath)> <!--- get and read the CSV-TXT file ---> <cffile action="read" file="#currentDirectory#/smalltest.csv" variable="csvfile"> <!--- create a new array ---> <cfset array=ArrayNew(2)> <!--- loop through the CSV-TXT file on line breaks and insert into database ---> <cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#"> <cfset array[#index#][1]=#listgetAt('#index#',1, ',')#> <cfset array[#index#][2]=#listgetAt('#index#',2, ',')#> </cfloop> <cfdump var=#array#> 

Bonus:

On the side of the note, this will save me a lot of time if there was some way to call the PHP file from ColdFusion, since I already have this whole script (this is just a small part) in PHP. I read about special ColdFusion tags (the <cf_php> tag would work fine for me), but admin says no, so I have to work with ColdFusion or find a way to render PHP through ColdFusion. A framework, JavaScript, or the <cfhttp> is all I think might work ... if you have an idea, let me know.

+3
source share
2 answers

Actually, I think you could simplify Henry's example using another array and arrayAppend.

 <cfset array=ArrayNew(1)> <cfloop index="line" list="#csvfile#" delimiters="#chr(10)##chr(13)#"> <cfset arrayAppend(array, listToArray(line))> </cfloop> 

A scalar value of type coldfusion.runtime.Struct cannot be an array assigned to a two-dimensional ColdFusion.

FYI: The source code represents the types of mixing cycles. With <cfloop list=".."> value of index is an element of a list of type "a, b" (and not a line number). Obviously, "a, b" is not an expected numeric index, hence an error.

 <!--- what the code is actually doing ---> <cfset array['a,b'][1]=#listgetAt('#index#',1, ',')#> <cfset array['a,b'][2]=#listgetAt('#index#',2, ',')#> <cfset array['c,d'][1]=#listgetAt('#index#',1, ',')#> .... 

Having nothing to do with your mistake, none of these signs are required. The code will work anyway, but writing more cleanly:

 <cfset array[lineNum][1]= listgetAt( index, 1, ',')> 

instead

 <cfset array['#lineNum#'][1]=#listgetAt('#index#',1, ',')#> 
+5
source
 <cfset array=ArrayNew(2)> <cfset lineNum=1> <cfloop index="line" list="#csvfile#" delimiters="#chr(10)##chr(13)#"> <cfset array[lineNum] = listToArray(line)> <cfset lineNum = lineNum + 1> </cfloop> 
+1
source

Source: https://habr.com/ru/post/1385486/


All Articles