• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Conditional loop

New Here ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

Hi,

I have what I thought was a simple conditional loop to find each occurence of a serach string within a variable. Basically trying to find the occurence, note it, then move on. If findnocase can't find an occurence, it should return 0, ending the loop:

<cfset findvar = 1>

<Cfloop condition="findvar GT 0">
<cfoutput>
<cfset findvar = #findnocase("#form.searchterm#","#searchfile#",#findvar#)#>
Position in file: #findvar#<br>

</cfoutput>
</cfloop>

This doesn't work at all. Just spins forever and never returns a result.

...help? Thanks!
TOPICS
Advanced techniques

Views

385

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 07, 2009 Jan 07, 2009
<Cfloop condition="findvar GT 0">
<cfset findvar = findnocase(form.searchterm, searchfile, findvar+1)>
Position in file: <cfoutput>#findvar#</cfoutput><br>
</cfloop>

Votes

Translate

Translate
Community Expert ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

<Cfloop condition="findvar GT 0">
<cfset findvar = findnocase(form.searchterm, searchfile, findvar+1)>
Position in file: <cfoutput>#findvar#</cfoutput><br>
</cfloop>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

This worked great...thanks so much.

Though I'm not sure why mine didn't work (something with the variables..?)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

LATEST
quote:

Originally posted by: pkonshak
This worked great...thanks so much.

Though I'm not sure why mine didn't work (something with the variables..?)

Your's didn't work because you didn't change the value of your variable each time through the loop.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 07, 2009 Jan 07, 2009

Copy link to clipboard

Copied

pkonshak wrote:
> This worked great...thanks so much.
>
> Though I'm not sure why mine didn't work (something with the variables..?)

You didn't have the findvar + 1. So subsequent searches, after the fist
loop iteration, started right at the first found string. Thus finding
it again and again and again and again and again... I think you get the
idea.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation