0

I have an array of numbers, while looping through array I want to output two elements at the time. Here is my array:

<cfset myArray = [74539,1500285,1334095,1500293,1334096,1500294,1334098,1500295,1334109,1500296]>

Here is my loop:

<cfoutput>
    <cfloop from="1" to="#arraylen(myArray)#" index="a">
        <cfset currAssignID = firstAssignList[a]>
        <cfset nextAssignID = firstAssignList[a+1]>
        #currAssignID# - #nextAssignID#<br>
    </cfloop>
</cfoutput>

Code above will produce this output:

74539 - 1500285
1500285 - 1334095
1334095 - 1500293
1500293 - 1334096
1334096 - 1500294
1500294 - 1334098

As you can see my code is outputting previous number every time, I would like to see this:

74539 - 1500285
1334095 - 1500293
1334096 - 1500294
1334098 - 1500295
1334109 - 1500296

If anyone knows where my code is breaking please let me know. Thank you.

3
  • 5
    Use the step attribute on the loop and set it to 2. It will just skip an index giving you the result you want. Commented Mar 21, 2017 at 17:30
  • I nominate #haxtbh to answer the question. Commented Mar 21, 2017 at 17:56
  • If numbers 1&2 and 3&4.... are related, can you modify your array to simplify that. Then you could simply loop through the top array and output each element. ie myArray = [[1,2],[3,4],...]. Then loop through myArray and output [1][1]-[1][2]...[2][1]-[2][2].... Commented Mar 21, 2017 at 20:05

1 Answer 1

3

You can use the step attribute on the loop to specify the increment that the loop goes through the elements. As you want to output them in pairs you can set step="2". This will skip every other element.

<cfset myArray = [74539,1500285,1334095,1500293,1334096,1500294,1334098,1500295,1334109,1500296,1334110,1500297,1334117,1500298,1334124,1500299,1334138,1500286,1334139,1500287,1334140,1500288,1337768,1500289,1338779,1500290,1338783,1500291,1338801,1500292]>


<cfoutput>
    <cfloop step="2" from="1" to="#arraylen(myArray)#" index="a">
        <cfset currAssignID = myArray[a]>
        <cfset nextAssignID = myArray[a+1]>
        #currAssignID# - #nextAssignID#<br>
    </cfloop>
</cfoutput>

This will give the results:

  74539 - 1500285
  1334095 - 1500293
  1334096 - 1500294
  1334098 - 1500295
  1334109 - 1500296
  1334110 - 1500297
  1334117 - 1500298
  1334124 - 1500299
  ... and so on

You can see an example here - http://trycf.com/gist/32a57dc787ba2756c88b4d1b74e3917c/acf11?theme=monokai

Sign up to request clarification or add additional context in comments.

4 Comments

Side note, if the array can contain an odd number of elements, add a size check so the code does not error out on the last element.
@Leigh checking the size making sure is not empty or that always have to be even number of elements in the array?
Just checking the element exists. When you output the a+1, if it doesn't exist it will error. If you run my example above without the step attribute you will see the error. You could do mod on the arrayLen to make sure it's even numbered too.
@espresso_coffee - Check the size within the loop to avoid an error when using myArray[a+1] if the total number of elements is uneven: trycf.com/gist/31ddf986ba2b512ef2163aeb7b3dcbd1/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.