Railo 4.0 Previews: For/In loops

June 1, 2012 · By Mark Drew · 3 Comments

In Railo 4.0 we have expanded the capabilities of using the cfscript syntax. As part of this we devoted some attention to how you can loop through collections using the for(x in y){} syntax.

We have also changed how we loop over queries, now giving you access to the row itself!

Let's check out some examples, first up looping through a structure:

myStruct = {one: "The One" ,two: "The Two",three: "The Three",four:"The Four",five: "The Five"};

for(key in myStruct){
   dump(key);
}

This dumps out the following:

We can do this for arrays too!

myArray = ListToArray("one,two,three,four,five");
for(item in myArray){
   dump(item);
}

This dumps out the following:

And finally, we don't forget Queries! (notice the creation syntax! Lovely!)

myQuery = Query("id":[1,2,3,4,5],value:[1,2,3,4,5]);
for(row in myQuery){
   dump(row);
}

This dumps out the following:

Tags: Preview

3 responses so far ↓

  • 1 Jean Moniatte // Jun 1, 2012 at 5:18 PM

    Getting better every day! When looping over a query, is the myQuery.currentRow variable still available?
  • 2 Mark Drew // Jun 1, 2012 at 5:20 PM

    Yes, but you have to name it after the query:

    for(row in myQuery){
          dump(myQuery.currentRow);
          dump(myQuery.columnList);
          dump(row);
       }
  • 3 Serge // Jun 7, 2012 at 5:24 PM

    Hello, what about items.each( function... )?

Leave a Comment

Leave this field empty: