ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Iron Speed
Go Back   Dev Shed ForumsProgramming Languages - MoreColdFusion Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old September 1st, 2004, 04:13 AM
bwinter bwinter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 bwinter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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>

Reply With Quote
  #2  
Old September 1st, 2004, 06:56 AM
DEfusion DEfusion is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 288 DEfusion User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 h 26 m 45 sec
Reputation Power: 6
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

Reply With Quote
  #3  
Old September 1st, 2004, 08:11 AM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
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

Reply With Quote
  #4  
Old September 1st, 2004, 01:54 PM
DeepDown DeepDown is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Netherlands
Posts: 99 DeepDown User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 4 h 1 m 8 sec
Reputation Power: 6
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*]

Reply With Quote
  #5  
Old September 1st, 2004, 02:18 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,627 kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level)kiteless User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 10 h 8 m 55 sec
Reputation Power: 53
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>

Reply With Quote
  #6  
Old September 1st, 2004, 07:57 PM
bwinter bwinter is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 2 bwinter User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
  #7  
Old September 13th, 2004, 09:05 AM
pmolaro pmolaro is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Location: North Carolina
Posts: 1 pmolaro User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > cfloop over csv file with empty fields


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway