ColdFusion Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
  #1  
Old February 9th, 2005, 10:11 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
How can I check/verify array values?

I have a thing where the user uploads a tab-delimited text file with 4 "columns" for each "row" which I turn into a multi-dimensional array. Everything works smashingly except for one thing--if one row in the text file doesn't have all four fields, the program bombs (because the code is looking for a variable that isn't there).

I'm wondering how to check if an element in the array exists or not. I can't do <CFIF #aCaseNumbers[1][4]# IS ""> because if the first row of the text file only had 3 values, #aCaseNumbers[1][4]# was never created in the first place.

I tried ParameterExists and IsDefined, but neither one worked. Here's the last code I tried to give you an idea what I want to do:
Code:
<CFOUTPUT>
<CFLOOP INDEX="i" FROM="1" TO="#ArrayLen(aCaseNumbers)#">
     <CFIF IsDefined("#aCaseNumbers[i][1]#")><CFELSE>bad<CFABORT></CFIF>
     <CFIF IsDefined("#aCaseNumbers[i][2]#")><CFELSE>bad<CFABORT></CFIF>
     <CFIF IsDefined("#aCaseNumbers[i][3]#")><CFELSE>bad<CFABORT></CFIF>
     <CFIF IsDefined("#aCaseNumbers[i][4]#")><CFELSE>bad<CFABORT></CFIF>
</CFLOOP>
</CFOUTPUT>
The error I get for that one is:

An error occurred while evaluating the expression:
IsDefined("#aCaseNumbers[i][1]#")
Error near line 69, column 8.
Parameter 1 of function IsDefined which is now "111-1111111" must be a syntactically valid variable name.

Any advice? I'm wondering if I'm just missing some syntax or something. Thanks.

Reply With Quote
  #2  
Old February 9th, 2005, 10:29 AM
Shane_Z Shane_Z is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: In denial
Posts: 22 Shane_Z User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 57 m 52 sec
Reputation Power: 0
Send a message via MSN to Shane_Z
if it is a string value you are checking just check the length of the string to make sure its greater than 1.

Code:
..
<CFIF Len(aCaseNumbers[i][1]) GT 0><CFELSE>bad<CFABORT></CFIF>
..

Reply With Quote
  #3  
Old February 9th, 2005, 10:53 AM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
Thanks, but that's the problem I'm having. If there was no 4th field in a row in the text file, the 4th element in the array is never created, so I can't check it because it doesn't exist. This is the error that gives:

The element at position 4 in dimension 2 of object "aCaseNumbers" cannot be found. The object has elements in positions 1 through 3. Please, modify the index expression.

Reply With Quote
  #4  
Old February 9th, 2005, 11:05 AM
Shane_Z Shane_Z is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Location: In denial
Posts: 22 Shane_Z User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 11 h 57 m 52 sec
Reputation Power: 0
Send a message via MSN to Shane_Z
Quote:
Originally Posted by Shmoo
The element at position 4 in dimension 2 of object "aCaseNumbers" cannot be found. The object has elements in positions 1 through 3. Please, modify the index expression.


You need to make sure all your structure elements are inserted when you add your data. That is where your problem is. I usually insert blank values into the row then go back and update the values as needed.

Reply With Quote
  #5  
Old February 9th, 2005, 12:50 PM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
Right, and that's what I was thinking, but what's the best way to do that? Here's my code for building the array
Code:
<cfset aCaseNumbers = arrayNew(2)>
		
<CFLOOP INDEX="breaks" LIST="#UploadedFile#" DELIMITERS="#chr(13)##chr(10)#">
	<CFSET arrayAppend(aCaseNumbers, arrayNew(1))>
		<CFLOOP INDEX="tabs" LIST="#breaks#" DELIMITERS="#chr(9)#">
		<CFSET arrayAppend(aCaseNumbers[arrayLen(aCaseNumbers)], tabs)>
		</CFLOOP>
</CFLOOP>

Would I just build a blank array before that? This is my first time doing stuff like this with arrays, so I'm not sure how to go about this...

I still think IsDefined or ParameterExists should work :P

Reply With Quote
  #6  
Old February 9th, 2005, 01:27 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,689 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 16 h 33 m 51 sec
Reputation Power: 53
I still don't understand how you're going to know which one of the elements the user omitted. If you have a line that should have 10 tab-delimited elements in it, but the user submits a line with only 9 elements in it, how do you know which one they didn't include?
__________________
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
  #7  
Old February 9th, 2005, 02:33 PM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
I know what you mean, but that's not a concern. Basically, each row in the text file should have 4 things:

case number [tab] lender [tab] loan officer [tab] loan number
case number [tab] lender [tab] loan officer [tab] loan number
case number [tab] lender [tab] loan officer [tab] loan number
case number [tab] lender [tab] loan officer [tab] loan number
etc...

If a row doesn't have 4 entries, I want to give an error saying the text file was invalid or whatever, it doesn't matter what wasn't there, just so long as I know that something wasn't there.

Now that I write that, is there an easy way to check each row of the array? ArrayLen is for the whole array, is there something that counts the elements in each row in a multidimensional array? Something like <CFIF (elements in first row of array) LT 4> is all I would need.

Reply With Quote
  #8  
Old February 9th, 2005, 02:41 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,689 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 16 h 33 m 51 sec
Reputation Power: 53
Yes you can test the length using listLen() or arrayLen() (after it's placed into an array of course).

Reply With Quote
  #9  
Old February 9th, 2005, 02:52 PM
Shmoo Shmoo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 38 Shmoo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 43 m 17 sec
Reputation Power: 4
Fantastic, thanks! I guess I should have tried arrayLen() earlier, but like I said I thought that was for the WHOLE array. I didn't know you could specify rows and stuff. Good to know

Here's the final code in case anyone searches for something like this:

<CFLOOP INDEX="i" FROM="1" TO="#ArrayLen(aCaseNumbers)#">
<CFIF arrayLen(aCaseNumbers[i]) LT 4>error message here<CFABORT><CFELSE></CFIF>
</CFLOOP>

Reply With Quote
  #10  
Old February 9th, 2005, 04:17 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,689 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 16 h 33 m 51 sec
Reputation Power: 53
Just a thought, but aborting the whole request might be a bit extreme. As a future enhancement you might look into exception handling (which is what this really is, there are distinct differences between an exception and a real error). You could use <cfthrow> to throw an exception, catch it using <cftry> and <cfcatch>, and redisplay the form for example.

Reply With Quote
  #11  
Old February 9th, 2005, 04:19 PM
kiteless kiteless is offline
Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jun 2002
Location: Raleigh, NC
Posts: 3,689 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 16 h 33 m 51 sec
Reputation Power: 53
As an example:

<cftry>
<CFLOOP INDEX="i" FROM="1" TO="#ArrayLen(aCaseNumbers)#">
<CFIF arrayLen(aCaseNumbers[i]) LT 4><cfthrow type="invalidData" message="You must specify 5 case numbers"></CFIF>
</CFLOOP>
<cfcatch type="invalidData">
...include the form again, redirect to the form page, etc...
</cfcatch>
</cftry>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreColdFusion Development > How can I check/verify array values?


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 5 hosted by Hostway
Stay green...Green IT