View comments | RSS feed

ArraySet

Description

In a one-dimensional array, sets the elements in a specified index range to a value. Useful for initializing an array after a call to ArrayNew.

Returns

True, on successful completion.

Category

Array functions

Function syntax

ArraySet(array, start_pos, end_pos, value)

See also

ArrayNew; Populating arrays with data in Using Arrays and Structures in ColdFusion MX Developer's Guide

History

ColdFusion MX: Changed behavior: this function can be used on XML objects.

Parameters

Parameter Description

array

Name of an array.

start_pos

Starting index position of range to set.

end_pos

Ending index position of range to set. If this value is greater than array length, ColdFusion adds elements to array.

value

Value to which to set each element in the range.

Example

<h3>ArraySet Example</h3>

<!--- Make an array --->
<cfset MyNewArray = ArrayNew(1)>
<!--- ArrayToList does not function properly if the Array has not been initialized
with ArraySet ---> <cfset temp = ArraySet(MyNewArray, 1,6, "Initial Value")> <!--- set some elements ---> <cfset MyNewArray[1] = "Sample Value"> <cfset MyNewArray[3] = "43"> <cfset MyNewArray[6] = "Another Value"> ...

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


campem said on Apr 9, 2008 at 2:04 PM :
I used this function for the first time and stumbled upon an unexpected behavior.

When using ArraySet to initialize an array, if the "value" is a struct, or in my case the return value of the structNew function then the array cells are initialized with the reference to a single struct, which means that modifiying the content of any array cell modifies the refernced struct hence all array cells.

This is unexpected because I was assuming that ArraySet would set the cells by vakue not reference even for complex objects.

Code example:

<cfscript>
example = arrayNew(1);
ArraySet(example, 1, 10, structNew());
for (i=1; i LTE ArrayLen(example ); i=i+1)
{
example[i].id = i;
}
</cfscript>
<cfdump var="#example#" />
halL said on Apr 11, 2008 at 6:50 AM :
This behavior is expected, as ColdFusion accesses structures by reference.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000393.htm