Array Loop "There is no property with name" Error

I’m parsing a string to get hashtags and I’m running into a problem I don’t understand. (i’ve been away from coding for a while so…)

I need to grab the position of the start of the hashtag -1

<cfset local.posttext = "This is a post. There is a ##hashtag and ##another and ##onemore">

<cfset bstart2 = refindnocase("##", local.posttext,1, true, "all")>

<cfloop from="1" to="#arrayLen(bstart2)#" index = "x">
	<cfset variables['bStart' & #x#] = bstart2[#x#].pos[1]-1>
         <!--- more variable setting code --->
</cfloop>

When there are 1 or 2 hashtags in the string, it works fine. When I add the 3rd it fails with the error

there is no property with name [3] found in [int]

arrayLen(bstart2) returns 3 as expected

Dumping bstart2 shows the 3rd array has exactly the same structure as 1 & 2.

This also works
<cfset variables['bStart' & #x#] = 'foo'>

So what is it about

bstart2[#x#].pos[1]-1

that’s causing the error

even hard coded it fails

bstart2[3].pos[1]-1

There is a [3].pos[1] value so I can’t figure out why it’s not working.

I just tried the code on cffiddle and I get a different error

You have attempted to dereference a scalar variable of type class java.lang.Double as a structure with members.

but I still don’t get why

That code you posted doesn’t error in the way you suggest it does.

Can pls you post some code that does error in the way you say it does…

Thanks but it’s a literal copy and paste from my test code so it -is- erroring like that. I specifically removed the other code to ensure nothing else was causing the problem.
As noted, it also fails on cffiddle. (which from the error style is running ACF i guess?)

I can’t post the screenshot of the lucee error until I get home but it is erroring.

I have no idea why it’s not working for me or cffiddle but is on trycf

Here is the code and error message side by side just now.

I did copy and paste the exact code (in case of some weird character encoding or something) into TryCF and there is no error but it errors on my server and on CFFiddle.

and FWIW, here’s the result when there’s only 2 hashtags/array items.

Works fine.

Edit…works fine except… why is index X = 3 when arrayLen = 2?

Screenshot 2024-08-28 154435

Please always post all your code… also the one that displays the wrong array length. Please, if you have questions to outputs, always show exactly the code that outputs it, and NOT any edited variants where it’s clear the code running is not the same that is shown by the output.

1 Like

I guess I should have not added the comment line about variable setting. The lines of code I posted -is- the code that is causing the error. I stripped out my other code as part of my debug process. I have -not- removed something that might be a cause. When I run these 5 lines, I get the error as shown in the screenshot of the side by side.

And to be clear this code all by itself throws the error

<cfset local.posttext = "This is a post. There is a ##hashtag and ##another and ##onemore ">
<cfset bstart2 = refindnocase("##", local.posttext,1, true, "all")>
<cfloop from="1" to="#arrayLen(bstart2)#" index = "x">
	<cfset variables['bStart' & x] = bstart2[x].pos[1]-1>
</cfloop>

To show the incorrect array length I just removed one of the hashtags from postText. The arrayLen should be 2 but x = 3. I have no idea if that related but it’s weird.

There is some confusion. To me at least. Then again, perhaps Lucee is just as confused as I am. :slightly_smiling_face:

The variable bstart2 is an array. Within the loop, when the index x is 2, there is an attempt to assign a simple value bstart2[2].pos[1]-1 to the variable variables.bStart2. This new variable shares the same name as the array. Hence my confusion.

If all you want is to obtain the positions, then you should separate concerns by creating a new name for the position variable. Something like this:

 <cfset local.posttext = "This is a post. There is a ##hashtag and ##another and ##onemore and ##yetonemore">

<cfset bstart2 = refindnocase("##", local.posttext,1, true, "all")>

<cfloop from="1" to="#arrayLen(bstart2)#" index = "x">
	<cfset variables['bStartPos' & x] = bstart2[x].pos[1]-1>
	<cfoutput>x=#x#; variables['bStartPos' & x]=#variables['bStartPos' & x]#</cfoutput><br>
</cfloop>

OK…That’s sorted it. Thanks!

I thought that the bStart2 array would be overwritten but I think I get that using variables[‘bStart’ & x] tries to modify the array variable causing the error.

I guess I don’t understand why the code fails (as it apparently should) on my server and on cffiddle but worked on TryCF when it should not have? That was a layer of confusion on top of things.

In any case, just renaming the array allows the code to work fine

<cfset local.posttext = "This is a post. There is a ##hashtag and ##another and ##onemore and ##evenmore">
<cfset tmpArr = refindnocase("##", local.posttext,1, true, "all")>
<cfloop from="1" to="#arrayLen(tmpArr)#" index = "x">
	<cfset variables['bStart' & x] = tmpArr[x].pos[1]-1>
</cfloop>
<cfdump var="#variables#">

Hair pulling ceased. Thanks again.