|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
cfloop over csv file with empty fields
hi thi sis my forst message ever on this board
I am trying to parse a csv file that i have uploaded to my webserver, problem is, i am trying to allow for possible empty fields that could eb included with my csv file. currently when i loop over each row, and then the cells inside to put them into an array, if the csv row has empty fields but still has the delimiters: eg 1,2,,4,5 the resulting array has only 4 rows instead of 5. my code is below if anyone can please help: <cfset CRLF = Chr(13) & Chr(10)><!--- Linefeed & carriage return ---> <cffile action="READ" file="#ExpandPath('/pharma/uploads')#/Book1.csv" variable="csvData"> <cfloop index="csvRecord" list="#csvData#" delimiters="#CRLF#"> <cfset csvArray = ArrayNew(1)> <cfset csvCellCount = 1> <cfloop index="csvCell" list="#csvRecord#" delimiters=","> <cfset csvArray[variables.csvCellCount] = csvCell> <cfset csvCellCount = csvCellCount + 1> </cfloop> <cfdump var="#csvArray#"> <!--- just dump an array here to see what data we have ---> </cfloop> |
|
#2
|
|||
|
|||
|
Other than a custom tag that doesn't concatenate empty list elements you could use a trick that I've used a couple of times.
Insert a random character after each delemeter (e.g. the |) this is not going to be in your data. So you'd do a replace of ',' with ',|'. This way every element will have some content, you obviously have to remove this fake content you placed into the list values before you can do anything useful with the data. -D |
|
#3
|
|||
|
|||
|
This behavior is by design. CF automatically interprets multiple consecutive delimiters as a single delimiter. If you want to force it to use every delimiter, you'll probably have to loop over the string character by character and look for commas, store their positions, and then use those positions to break up the string. You could also try replacing consecutive commas with a dummy character like '~,' but this could be inaccurate if there are an odd number of consecutive delimiters.
__________________
Ask if you have a question, but also help answer questions that you have knowledge of! Thanks, Brian. How to Post a Question in the Forums |
|
#4
|
|||
|
|||
|
you could do some typecasting up and back:
<cfset my="1,2,,4,5"> <cfset my=ListToArray(my)> <cfset my=ArrayToList(my)>
__________________
** Don't expect me to code your needs, but if I am able to help, I'm willing. Shout, grab and use the hand! ** Man can no more own the land we walk upon, as they can lay claim on the air that we breath ** DeepDown I'm addicted to structures.... ohw and music ![]() ** Almost forgot I had an account here [*o*] |
|
#5
|
|||
|
|||
|
I think he wants to keep 5 elements in the list and just have one be blank. What about something like this:
<cfset my="1,2,,4,5"> <cfset my = replace( my, ',', ' ,', 'All' ) /> <cfoutput>List "#my#" has #listLen( my, ',' )# elements</cfoutput> <br> <cfoutput>Looping:<br> <cfloop index="thisElement" list="#my#" delimiters=","> #trim( thisElement )#<br> </cfloop> </cfoutput> |
|
#6
|
|||
|
|||
|
Thanks for all the replys. I actually had someone give me a link to a udf called listfix, it does the trick nicely !
http://www.cflib.org/udf.cfm/listfix CFLib.org - ListFix |
|
#7
|
|||
|
|||
|
I Used a Space
I was having the same problem with cookie values. I had a form, that I wanted to be pre-populated with my cookie values (I store all my field values in one cookie as a list, instead of multiple cookies). So what I was doing was using ListToArray to break my cookie value out to an array. Then each field value had a defalut value of Array[1], Array[2] and so on.
Probelm was, if a field was left blank, ListToArray ignored it as you describe. This left the remain fields off by one (so Array[5] had Array[6]'s value) and the last one threw an error b/c it couldn't find its value at all. What I did to trick it was to add a "space" to each default value, like: Code:
<cfinput name="address2" type="text" size="30" required="no" value=" #aShipInfo[4]#"> So, now when i create/re-creat the cookie values, a "sudo-empty" slot is forced into the Array index where the empty value lives. This seems to work fine. If anybody sees a problem here, please let me know. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > cfloop over csv file with empty fields |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|