Copy link to clipboard
Copied
Here's my code:
<cfif StructKeyExists(cfData.data, "inherited_roles") || IsArray(cfData.data.reader_roles)>
<cfset arrayRoles = "reader_roles" >
<cfif ArrayIsEmpty(cfData.data.inherited_roles)>
<cfset arrayRoles = "inherited_roles" >
</cfif>
<cfloop index="p" from="1" to="#arrayLen(cfData.data.arrayRoles)#">
<!---some code here--->
</cfloop>
</cfif>
And here's the error I kept getting.
Element ARRAYROLES is undefined in a CFML structure referenced as part of an expression.
The error occurred on line 07 when I have the #arrayLen(cfData.data.arrayRoles)#.
Any suggestion is much appreciated
Okay, I think I got it working now.
<cfif not len(cfData.data.reader_roles)>
<cfset arrayRoles = cfData.data.inherited_roles >
<cfelse>
<cfset arrayRoles = cfData.data.reader_roles >
</cfif>
<cfloop index="p" from="1" to="#arrayLen(arrayRoles)#">
<!---code goes here--->
</cfloop>
Looks like the not len(cfData.data.reader_roles) do test if the array is empty or not. I'm not sure why the ArrayIsEmpty is not working in this case.
Copy link to clipboard
Copied
The error is telling you the problem.
Replace "arrayRoles" with "reader_roles" on line 7 and you won't get the error unless "cfData.data.reader_roles" is not an array, which could happen because of line 1.
Perhaps you should explain what you are trying to achieve, perhaps there is a better way.
Cheers
Eddie
Copy link to clipboard
Copied
First of, line 4 should be: <cfif ArrayIsEmpty(cfData.data.reader_roles)>
Okay, by default, I am setting the cfData.data to reader_roles in line 4. However, if reader_roles array is empty, then set the arrayRoles variable to inherited_roles.
So in cfloop line 7, I want to loop through only the array that actually have some data in there. Another way would be to do an if-else statement and put all the codes in there but I was thinking that by simply swapping the "reader_roles" for "inherited_roles" and vice versa at the end of cfData.data would work but ColdFusion throws an error.
Copy link to clipboard
Copied
You're assigning a string value to arrayRoles and then trying to use it as an array.
Try the following instead (assuming both are indeed arrays):
<cfif IsArray(cfData.data.inherited_roles) || IsArray(cfData.data.reader_roles)>
<cfif ArrayIsEmpty(cfData.data.reader_roles)>
<cfset arrayRoles = cfData.data.inherited_roles >
<cfelse>
<cfset arrayRoles = cfData.data.reader_roles >
</cfif><cfloop index="p" from="1" to="#arrayLen(arrayRoles)#">
<!---some code here--->
</cfloop></cfif>
Cheers
Eddie
Copy link to clipboard
Copied
This code:
<cfif ArrayIsEmpty(cfData.data.reader_roles)>
<cfset arrayRoles = cfData.data.inherited_roles >
<cfelse>
<cfset arrayRoles = cfData.data.reader_roles >
</cfif>
Causes this error:
Object of type class java.lang.String cannot be used as an array
Copy link to clipboard
Copied
Here is a working example, modify to suite your needs:
<cfscript>
i = 1;cfData.data = arrayNew(1);
cfData.data = structNew();cfData.data.inherited_roles = arrayNew(1);
cfData.data.reader_roles = arrayNew(1);cfData.data.reader_roles[1] = 'reader role 1';
cfData.data.reader_roles[2] = 'reader role 2';if ( isArray(cfData.data.inherited_roles) || IsArray(cfData.data.reader_roles) ) {
if ( arrayIsEmpty(cfData.data.reader_roles) )
arrayRoles = cfData.data.inherited_roles;
else
arrayRoles = cfData.data.reader_roles;for (nIndex=1; nIndex <= arrayLen(arrayRoles); nIndex++) {
writeOutput(arrayRoles[nIndex]);
writeOutput('<br>');
}
}
</cfscript>
Cheers
Eddie
Copy link to clipboard
Copied
I have a question on this line of code.
ArrayIsEmpty(cfData.data.reader_roles)
If I do IsArray(cfData.data.reader_roles) it returns true. It is in fact an array but when I do ArrayIsEmpty, it gave me the error below.
Object of type class java.lang.String cannot be used as an array
So, if it's an array, how do I test if it's an empty array or not in ColdFusion? I like to understand why it's giving me the error despite being an array.
Copy link to clipboard
Copied
The following snippet works as expected.
testArray = arrayNew(1);
if ( isArray(testArray) ) {
writeOutput('testArray is an array.<br>');
if ( arrayIsEmpty(testArray) )
writeOutput('testArray is empty.<br>');
else
writeOutput('testArray is not empty.<br>');
}
Can you provide a similar snippet that demonstrates the failure you are talking about?
Cheers
Eddie
Copy link to clipboard
Copied
I even tried this <cfif #arrayLen(cfData.data.reader_roles)# lte 0 > and it error out as well.
Error in custom script module
Copy link to clipboard
Copied
Okay, I think I got it working now.
<cfif not len(cfData.data.reader_roles)>
<cfset arrayRoles = cfData.data.inherited_roles >
<cfelse>
<cfset arrayRoles = cfData.data.reader_roles >
</cfif>
<cfloop index="p" from="1" to="#arrayLen(arrayRoles)#">
<!---code goes here--->
</cfloop>
Looks like the not len(cfData.data.reader_roles) do test if the array is empty or not. I'm not sure why the ArrayIsEmpty is not working in this case.
Copy link to clipboard
Copied
As I said, please show a reproducible snippet of the failure, unless you feel this is no longer an issue, in which case please close this thread. It is best for the community to keep each thread focused on a single issue.
Cheers
Eddie
Copy link to clipboard
Copied
It's no longer an issue. Looks like not len() works. I was just not sure why ArrayIsEmpty and arrayLen() is not working for testing if the array is empty.
Many thanks for your help though. I'd appreciated.