【发布时间】:2014-11-20 15:04:42
【问题描述】:
我正在尝试遍历一个 .csv 文件,这取决于一个列值,我进一步查看是否存在与该列中的值同名的文件
我使用的代码如下:
<cffile action="read" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csv" variable="csvfile">
<cfoutput>
<cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
<cfif FileExists(ExpandPath(listgetAt('#index#',5).txt))>
xxx
</cfif>
#listgetAt('#index#',1)#<br>
#listgetAt('#index#',2)#<br>
#listgetAt('#index#',3)#<br>
#listgetAt('#index#',4)#<br>
#listgetAt('#index#',5)#<br>
#listgetAt('#index#',6)#<br>
#listgetAt('#index#',7)#<br>
#listgetAt('#index#',8)#<br>
</cfloop>
</cfoutput>
但是当我运行它时,我得到以下错误:
You have attempted to dereference a scalar variable of type class java.lang.String as a structure with members.
The error occurred in C:/ColdFusion10/cfusion/wwwroot/kelly2/upload.cfm: line 6
4 : <cfloop index="index" list="#csvfile#" delimiters="#chr(10)##chr(13)#">
5 :
6 : <cfif FileExists(ExpandPath(listgetAt('#index#',5).txt))>
7 : xxx
8 : </cfif>
我已经尝试将 cfif 标签放入 try and catch 中,据谷歌称这解决了大多数人的问题,但它对我不起作用
【问题讨论】:
-
您还应该首先验证列表长度。在访问元素之前验证它是您所期望的。此外,如果您是 CF 新手,请注意大多数列表函数会忽略空元素。考虑改用数组。您可以使用
listToArray和 includeEmptyElements=true 保留任何空元素。与您的问题无关,但大多数#标志是不必要的。 -
鉴于您正在使用的内容的重要性,您绝对应该验证...很多。
-
嗨,莉!谢谢!这只是我为自己做的一个项目,所以幸运的是,我的失误不会导致经济崩溃,但是一旦我从 csv 中取出数据,我就会对数据进行验证
-
好吧,听着就放心了 ;-)
标签: coldfusion