ColdFusion Array Loop Works With ARGUMENTS

Posted December 27, 2007 at 7:38 AM

Tags: ColdFusion

As a quick follow up to my post yesterday on using ColdFusion 8's new array iteration via CFLoop, I thought I would check to see if array iteration worked on another "pseudo array", the function ARGUMENTS scope. The ARGUMENTS scope is a very interesting object that has both struct and array properties, meaning, it can be accessed both by key and by index value. As this object only has array-like behavior and is not a true array, it is not clear as to how much array manipulation will be possible. To test this, all I did was set up a function that takes its ARGUMENTS scope and loops over it, CFDumping out each value:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="LoopArguments"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="Iterates over the arguments array.">
  •  
  • <!--- Define the local scope. --->
  • <cfset var LOCAL = {} />
  •  
  • <!--- Iterate over the arguments as if it were an array. --->
  • <cfloop
  • index="LOCAL.Value"
  • array="#ARGUMENTS#">
  •  
  • <!--- Dump out data element. --->
  • <cfdump
  • var="[#LOCAL.Value#]"
  • label="ARGUMENTS Data Point"
  • />
  •  
  • </cfloop>
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>

In the CFDump tag, I am including the brackets for visible value deliniation; this means that only string values can be passed to the function, but this should work the same, functionally, with any type of value. Now, we just run the code:

 Launch code in new window » Download code as text file »

  • <!--- Call function with various values. --->
  • <cfset LoopArguments(
  • "Itty",
  • "Bitty",
  • "Kitty",
  • "Club"
  • ) />

It works perfectly, giving us the CFDump output:

[Itty] [Bitty] [Kitty] [Club]

Cool; ColdFusion 8's new array iteration works fine with the array-like behavior of the ARGUMENTS scope.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page


You Might Also Be Interested In:




Reader Comments

Dec 27, 2007 at 11:57 AM // reply »
97 Comments

I'd like to see you turn your prodigious investigative powers to what does and does not work with the cfscript loop:

for (i in someVar) { ... }

I know it works for structs (I use it a lot), but what else works with it.


Dec 27, 2007 at 1:09 PM // reply »
6,371 Comments

@Sean,

Not a bad idea. I rarely play around with CFScript, so it would be good to do some snooping. I know that you can finally use things like >= and <. Let me see what I can find out.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 7, 2009 at 5:53 PM
Ask Ben: Javascript String Replace Method
You can find here an advanced function that prepared with javascript replace function. This can make the first letters of words, sentences, lines and whatever you define automatically: http://www.m ... read »
Andrew Neely
Nov 7, 2009 at 4:56 PM
A Moment That Touched Me - The Fountainhead
Ben, Glad you enjoyed the podcast. Yeah, the Tank Riot guys can get really chatty during the episodes, but that's part of the charm of it for me. They've covered everything from Nichola Tesla to Cha ... read »
Nov 7, 2009 at 4:43 PM
Building A Fixed-Position Bottom Menu Bar (ala FaceBook)
Is it possible to make some more MenĂ¼`s ? ... read »
Jill
Nov 7, 2009 at 11:40 AM
How To Unformat Your Code (Like A Pro)
Derek, I think you might be right - sweet! Thanks for the link :) ... read »
Nov 7, 2009 at 11:25 AM
How To Unformat Your Code (Like A Pro)
I think it would be way easier to just use this http://www.logichammer.com/html-formatter/ He just released v3 and it rocks. ... read »
Jill
Nov 7, 2009 at 7:58 AM
How To Unformat Your Code (Like A Pro)
LMAO - this was pretty funny! I have to admit - I also love to reformat code so I can read it. My boss used to tell me to leave my OCD at home. Now I don't feel so bad after reading everyone else' ... read »
Nov 6, 2009 at 10:10 PM
How To Unformat Your Code (Like A Pro)
The timing of this post is just uncanny. I spent the last 15-20 minutes manually un-formatting my "Ben Nadel" style code within a CFC of mine. I was really digging the readability a few weeks ago, bu ... read »
Roe
Nov 6, 2009 at 5:11 PM
Passing Arrays By Reference In ColdFusion - SWEEET!
ArraySort also reorders the results of these java obj's ... read »