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




Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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,516 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 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »
Nov 21, 2009 at 12:28 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jody I can't seem to get your code sample to work. If you are still having problems, try this code out and see if it gets you what you wanted. <!--- Comma delimited list with various duplicates ... read »
Nov 21, 2009 at 11:03 AM
Groovy Operator Overloading Does Not Work In The ColdFusion Context
Hi Ben, Thanks for this informative post. Now I am reading ur old posts too ... read »