----- Original Message -----
From: "Dave Watts" <[EMAIL PROTECTED]>
To: "CF-Talk" <[EMAIL PROTECTED]>
Sent: Monday, November 17, 2003 9:16 AM
Subject: RE: Inserting rows into a 2d array
>
> Alternatively, why not just order the data the way you want it before
> building the array? Or, perhaps more importantly, why does the ordering
> matter at all? Ideally, in most (but not all) situations, it shouldn't.
What I'm trying to do is take a query result with two associated elements
named city_name and locationID. The two elements need to be displayed beside
each other, ordered alphabetically on the city name, with a blank line
between the end of one alpha group and the start of the next group (makes it
much easier to visually scan through a long list). The results will be
displayed in a four column, vertically ordered table. My idea is to insert
blank spacers into the results before the table is dynamically constructed
so that the table formatting is not messed up with the spacers running
horizontally across all four columns of the table.
AAA xxx CCC xxx
AAA xxx
DDD xxx
BBB xxx
BBB xxx EEE xxx
CCC xxx FFF xxx
> According to the documentation, existing elements should automatically be
> shifted up one in position, so you shouldn't be having this problem. What
> version of CF are you using?
Using CF5. The way I understand the documentation is that the existing
element is shifted one cell to the right, not up or down. In testing I found
this to be true. The existing element is moved to the right and the new
element is inserted as documentation states. This works whether the array is
1, 2 or 3 dimensional. Previously I thought the ArrayInsertAt was just
overwriting the element but now know it was shifting the existing element to
the right and not creating a new row as I was expecting.
So the problem is that I want insert a new ROW into a 2 dimensional array
not just add a new element (cell) to an existing row. This will create the
spacer between existing elements. Ideas or alternatives appreciated.
<!--- BEGIN test code for 1d array ------------------------->
<CFSET testArray1 = ArrayNew(1)>
<CFSET testArray1[1] = "Agness">
<CFSET testArray1[2] = "Albany">
<CFSET testArray1[3] = "Arlington">
<p>'testArray1' has #ArrayLen(testArray1)# elements.<br>
<cfloop index="Num" from="1" to="#ArrayLen(testArray1)#">
#Num# #testArray1[Num]#,
</cfloop></p>
<CFSET InsertSuccess1 = ArrayInsertAt(testArray1,3,"InsertAt3")>
<p>After insert 'testArray1' has #ArrayLen(testArray1)# elements.<br>
<cfloop index="Num1" from="1" to="#ArrayLen(testArray1)#">
#Num1# #testArray1[Num1]#,
</cfloop></p>
<!--- END test code --->
<!--- BEGIN test code 2d array ---------------------------->
<CFSET testArray2 = ArrayNew(2)>
<!--- <cfloop index="Num2" from="1" to="3"> --->
<CFSET testArray2[1][1] = "Agness">
<CFSET testArray2[2][1] = "Albany">
<CFSET testArray2[3][1] = "Arlington">
<CFSET testArray2[1][2] = "ID01">
<CFSET testArray2[2][2] = "ID02">
<CFSET testArray2[3][2] = "ID03">
<!--- </cfloop> --->
<p>'testArray2' has #ArrayLen(testArray2)# elements.<br>
<cfloop index="Num2" from="1" to="#ArrayLen(testArray2)#">
#Num2# #testArray2[Num2][1]#, #testArray2[Num2][2]#,
</cfloop></p>
<!--- insert new elements --->
<CFSET ArrayInsertAt(testArray2[3],1,"InsertHere")>
<p>After insert 'testArray2' has #ArrayLen(testArray2)# elements.<br>
<cfloop index="Num3" from="1" to="#ArrayLen(testArray2)#">
#Num3# #testArray2[Num3][1]#, #testArray2[Num3][2]#,
</cfloop></p>
<p>row 3 contents: #testArray2[3][1]#, #testArray2[3][2]#,
#testArray2[3][3]#</p>
<!--- END test code --->
------------ test results ------------------------------------
'testArray1' has 3 elements.
1 Agness, 2 Albany, 3 Arlington,
After insert 'testArray1' has 4 elements.
1 Agness, 2 Albany, 3 InsertAt3, 4 Arlington,
'testArray2' has 3 elements.
1 Agness, ID01, 2 Albany, ID02, 3 Arlington, ID03,
After insert 'testArray2' has 3 elements.
1 Agness, ID01, 2 Albany, ID02, 3 InsertHere, Arlington,
row 3 contents: InsertHere, Arlington, ID03
----------------------------------------------
>
> Dave Watts, CTO, Fig Leaf Software
> http://www.figleaf.com/
> voice: (202) 797-5496
> fax: (202) 797-5444
----- Original Message -----
> > It was suggested (thanks Chris) that I could insert elements
> > into an array by using the ArrayInsertAt() function:
> >
> > Example:
> > For a 1d array:
> > <cfset ArrayInsertAt(myArray, 5, "myValue")>
> >
> > For a 2d array:
> > <cfset ArrayInsertAt(myArray, 5, ArrayNew(1))>
> > <cfset myArray[5][1] = whatever>
> > <cfset myArray[5][2] = something_else>
> >
> > Problem is that 'ArrayInsertAt' will insert an element(s), but
> > it will overwrite what is currently at that location. What I
> > need to do is shift the rows of the Array down and insert a
> > new row containing the new elements. This adds to the array
> > content and retains the original content of the array.
>
[Todays Threads] [This Message] [Subscription] [Fast Unsubscribe] [User Settings]

