|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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>
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. |
|
#2
|
|||
|
|||
|
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> .. |
|
#3
|
|||
|
|||
|
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. |
|
#4
|
|||
|
|||
|
Quote:
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
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 |
|
#7
|
|||
|
|||
|
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. |
|
#8
|
|||
|
|||
|
Yes you can test the length using listLen() or arrayLen() (after it's placed into an array of course).
|
|
#9
|
|||
|
|||
|
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> |
|
#10
|
|||
|
|||
|
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.
|
|
#11
|
|||
|
|||
|
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> |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > ColdFusion Development > How can I check/verify array values? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|