• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to do arraySet in a two dimension array?

New Here ,
Apr 22, 2022 Apr 22, 2022

Copy link to clipboard

Copied

How to do arraySet in a two dimension array?

TOPICS
Advanced techniques

Views

108

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2022 Apr 22, 2022

Copy link to clipboard

Copied

To be clear, there's nothing about using arrayset for more than one dimension than you would for do for any cfml array function. For more on that, including examples, see the "other" docs for cf, the cf developers guide (versus the cfml reference), at:

 

https://helpx.adobe.com/coldfusion/developing-applications/the-cfml-programming-language/using-array... 

 

Let us know if that gets you going. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 22, 2022 Apr 22, 2022

Copy link to clipboard

Copied

Arrays are kind of complicated to use in CFML. Arrays themselves are pretty simple, but there are so many "helper" functions that you can frankly do without, and arraySet is one of them usually. While the arrayNew function lets you create "multidimensional" arrays, they're really just one-dimensional arrays that contain one-dimensional arrays as the values for each location in the outer array. The arraySet function is intended to work with a one-dimensional array, replacing code that would manually set each array position's value within an empty array. So, you could use arraySet with the "outer" array, or you could use it with an array in one of the positions in the outer array. It's only going to work with one or the other.

 

Here's an example that sets the outer array with default strings:

 

<cfset aExample = arrayNew(1)> <!--- note, this is a one-dimensional array --->

<cfset arraySet(aExample, 1, 5, "default string")>

 

Well, ok, that's not a two-dimensional array, is it? But you could have done something like this instead:

 

<cfset aExample = arrayNew(1)> <!--- note, this is STILL a one-dimensional array --->

<cfset aExampleNested = arrayNew(1)> <!--- so is this! --->

<cfset arraySet(aExample, 1, 5, aExampleNested)> <!--- now you have a two-dimensional array! --->

 

Of course, you really don't have to go through all that much trouble, you could just do this, which creates a one-dimensional array, sets the default value for each array member to be a nested array, and you end up with a two-dimensional array without seeing all the stuff happening behind the scenes:

 

<cfset aExample = arrayNew(2)> <!--- this is a two-dimensional array --->

 

The only difference between the last two examples is that the arraySet one explicitly specifies how many positions are created for the array. But since arrays in CF are dynamic - they automatically grow or shrink as things are added or removed - it's very rare that you'd care about this.

 

Finally, I would definitely recommend reading through the link that @Charlie Arehart provided, which is probably all that anyone really needs to know about arrays.

 

Dave Watts, Eidolon LLC

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 24, 2022 Apr 24, 2022

Copy link to clipboard

Copied

LATEST
quote

How to do arraySet in a two dimension array?


By @Alan4a

As Charlie and Dave have pointed out, and correctlly, the arraySet function is meant for a one-dimensional array. But that is not a shortcoming with regard to your question.

 

In ColdFusion a two-dimensional array is a one-dimensional array! It is a one-dimensional array whose elements are themselves one-dimensional arrays.

 

So, to use arraySet to initialize the elements of a 2-D array, you will have to follow a two-step process:

  1. Apply arraySet to initialize any of the sub-arrays that will be an element of the 2-D array;
  2. Apply arraySet to initialize the 2-D array, using as values the initialized 1-D sub-arrays from step1.

 

An example follows:

 

<cfscript>
// Define empty 2-dimensional array	
a2DArray=arrayNew(2);

/*
 Define sSubArray, an empty 1-dimensional array
  - intended to be a sub-array of a2DArray
  - to be populated with strings, the first 15 initialized to ""
*/
sSubArray=arrayNew(1);
arraySet(sSubArray, 1, 15, "");

/*
 Define nSubArray, an empty 1-dimensional array
  - intended to be a sub-array of a2DArray
  - to be populated with integers, the first 8 initialized to 0
*/
nSubArray=arrayNew(1);
arraySet(nSubArray, 1, 8, 0);


/*
 Define dtSubArray, an empty 1-dimensional array
  - intended to be a sub-array of a2DArray
  - to be populated with Datetime objects, 
    the first 6 initialized to null and the 7th to 12th values 
    initialized to the date now
*/
dtSubArray=arrayNew(1);
arrayset(dtSubArray,1,6,javacast('null',""));
arraySet(dtSubArray,7,12,now());

/*
Task: initialize a2DArray such that
 - its first 5 elements are sSubArray
 - the 6th to 10th elements are nSubArray
 - the 11th to 20th elements are dtSubArray
*/
arrayset(a2DArray,1,5,sSubArray);
arrayset(a2DArray,6,10,nSubArray);
arrayset(a2DArray,11,20,dtSubArray);

writedump(var=a2DArray, label="a2DArray initialized using arraySet")
</cfscript>

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation