Skip to main content
Known Participant
September 11, 2015
Answered

How to force my loop to start from position same as array?

  • September 11, 2015
  • 1 reply
  • 619 views

Hello everyone,

I'm working on my project and I have a problem looping through my array. As many of you know arrays in coldfusion start at position 1, inmy case if I put that my cfloop starts from 1 I'm getting an error because that does not match my array. Here is my sample of code:

<cfloop index="i" from="i-1" to=#(cnt)# step="1">

  <cfset myrow = #replace(myarray,chr(10),'')#>

  <cfset myrow = ListToArray(myrow,",",true)>

<cfoutput>

  (#myrow[1]#),

  (#myrow[2]#)

  <br/>

</cfoutoput>

</cfloop>

Does anyone know how I can fix this problem? I tried to use array in my loop but I got another error with that option.

Thanks in advance for any advise.

This topic has been closed for replies.
Correct answer WolfShade

You don't want your array to start at 1?  But it looks as if you're trying to get it to start from 0, which is out of index range.

If you want your loop to start after the first position, then you need to calculate that position and have that as the from value.

<cfloop index="i" from="{calculated position}" to="#cnt#" step="1">

HTH,

^_^

1 reply

WolfShade
WolfShadeCorrect answer
Legend
September 11, 2015

You don't want your array to start at 1?  But it looks as if you're trying to get it to start from 0, which is out of index range.

If you want your loop to start after the first position, then you need to calculate that position and have that as the from value.

<cfloop index="i" from="{calculated position}" to="#cnt#" step="1">

HTH,

^_^