Darren
Darren

Reputation: 11011

REFind Not Working Correctly

I am not sure if REFind is working correctly. I am basically trying to determine if a string starts with a certain name I perform some actions, but I am unsure if I can use REFind in a conditional sense. My code is below, and it should return at least 2 successful results.

<cfloop list="#form.fieldnames#" index="field">
 <cfif REFind("^xtra_" & i, field)>
  <cfset recipient = ArrayNew(1)>
  <cfset ArrayAppend(recipient, evaluate("xtra_#LOCAL.sanity.id[LOCAL.i]#_email-#i#"))>
  <cfset ArrayAppend(recipient, evaluate("xtra_#LOCAL.sanity.id[LOCAL.i]#_name_department-#i#"))>
  <cfset ArrayAppend(recipients, recipient)>
 </cfif>
 <cfset i=i+1>
</cfloop>
<cfdump var="#recipients#">
<cfabort>

Upvotes: 1

Views: 250

Answers (2)

ale
ale

Reputation: 6430

REFind returns the integer location of the string you are looking for. While you can use the ColdFusion shortcut where 0 equates to false and any other value equates to true (as you're doing), I suggest being more explicit in your conditional.

In other words...

REFind("^xtra_" & i, field) GT 0>
    <!--- string found --->

or

REFind("^xtra_" & i, field) EQ 0>
    <!--- string NOT found --->

Since you're trying to find a particular substring at the beginning of the string, perhaps something like <cfif REFind("xtra_" & i, field) EQ 1> might serve you better.

Upvotes: 1

Ben Doom
Ben Doom

Reputation: 7885

Offhand, I would say that i is not correctly set prior to the loop. I would add some debugging output inside the loop to tell me what it is doing.

If that doesn't help, please show us some sample form data (including formfields) and the results of your dump of recipients.

And while I'm at it, avoid evaluate(). You can reference the variables using bracket notation such as:

form["xtra_" & LOVAL.sanity.id[LOCAL] & "_email-" & i]

evaluate() is inherently inefficient, and can be a security hole, since it can execute arbitrary data. While it might not be a security issue here, it is best to avoid the problem altogether.

Upvotes: 1

Related Questions