Arrayfind in 2 dimensional array

<cfset da = arraynew(2)>
<cfset da[1][1] = "John">
<cfset da[1][2] = "8">
<cfset da[2][1] = "Mary">
<cfset da[2][2] = "7">

<cfdump var ="#da#">

-- how to find array position based on a value, in this case, say, "7" ?
arrayfind(da,"7") 
won't return correct result

thanks.

Because multidimensional arrays are arrays stored in arrays, you could get it with a loop, something like this for a two dimensional array:

<cfset da = arraynew(2)>
<cfset da[1][1] = "John">
<cfset da[1][2] = "8">
<cfset da[2][1] = "Mary">
<cfset da[2][2] = "7">
<cfset da[3][2] = "Mary">
<cfset strToFind="John">
<cfoutput>
<cfloop from="1" to="#arrayLen(da)#" index="primaryIndex">
	<cfloop from="1" to="#arrayLen(da[#primaryIndex#])#" index="secondaryIndex">
	Checking da[#primaryIndex#][#secondaryIndex#]<br>
	<cfif ArrayIsDefined(da[#primaryIndex#],#secondaryIndex#) and da[#primaryIndex#][#secondaryIndex#] is strToFind>
		 String found: da[#primaryIndex#][#secondaryIndex#]<br>
	</cfif>
	</cfloop>
</cfloop>
</cfoutput>

This search is case insensitve. You could build something similar inside a function.

1 Like

Interesting, thank you @andreas

Ok, I’m trying to parse a sentence, get word count for each unique word in the sentence. But it seems my logic is messed up. Please see below. Thanks.


<cfset s = "the oxford university mission find vaccine covid-19 virus race virus disappearing, time, adam hill, director oxford university's jenner institute, said weekend">

	<cfoutput>
		data to parse:#s# <br><br>
	</cfoutput>
	
	<cfset wCount = 0>
	<cfset wd = "">	
	<cfset wList = "">
	<cfset uwList = arrayNew(2)>
	<cfset uw = "">
	<cfset uwA = arrayNew(2)>
	
	<cfset count = 0>	
	<cfloop index=w list="#s#" delimiters=" ">
		<cfset count += 1>
		
		<!--- create word list & seed unique word list 2d array, "uwA" --->
		<cfif !listFind(wList,w)>
			<cfset wd = "#w#">			
			<cfset wCount = 1>	
			
			<cfset wList = listAppend(wList,"#w#")>
			
			<cfset uwList[count][1] = "#w#">	
			<cfset uwList[count][2] = wCount>
				
				<cfif !listfind(uw,w)>					
					<cfset uw = listAppend(uw,"#w#",";")>
					
					<cfset uwA[count][1] = "#w#">
					<cfset uwA[count][2] = 1 >
										
					word found: <cfoutput>#w# </cfoutput> <br>
				</cfif>
			
		<cfelse>
			<cfset wCount += 1>	
			<cfset uwList[count][2] = wCount >
			
			<CFOUTPUT>
			<cfloop from="1" to="#arrayLen(uwA)#" index="primaryIndex">
				<cfloop from="1" to="#arrayLen(uwA[#primaryIndex#])#" index="secondaryIndex">
				Checking #uwA[#primaryIndex#][#secondaryIndex#]#<br>
				<cfif ArrayIsDefined(uwA[#primaryIndex#],#secondaryIndex#) and uwA[#primaryIndex#][#secondaryIndex#] IS w>
					 repeat word found: #uwA[#primaryIndex#][#secondaryIndex#]#<br>
					 <!--- increment the count of the repeat word --->
					 <cfset uwA[#primaryIndex#][2] = wCount> 
				</cfif>
				</cfloop>
			</cfloop>
			</CFOUTPUT>
						
		</cfif>
		
	</cfloop>
	
	uw: count = <cfoutput>#ListLen(uw,";")#</cfoutput>
	<br>
	<cfdump var="#uw#">
			
	<BR><BR>
	uwA: count = <cfoutput>#arrayLen(uwA)#</cfoutput>
	<br>
	<cfdump var="#uwA#">

See the answer of rynoguill at:

1 Like

Beautiful, thank you.

A reminder: You’ll need to tweak the function a little, because it finds the occurances, and not the amount of words. If you search for the term “ship” in the string “the ship shipped goods of a championship” it will find it 3 times. You could change it to reFind() function to find complete words only.

1 Like

Yeah, I’ve noticed that, thank you.