CFLib.org – Common Function Library Project

ArrayInsertArrayAt(a1, a2, pos)

Last updated September 13, 2001

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

Description:
Inserts an array at specified position in another array. Returns the resulting array. If you secifiy a position one greater than the length of the first array the second array will be concatenated to the first.

Return Values:
Returns an array.

Example:

view plain print about
<CFSCRIPT>
a1=ArrayNew(1);
a1[1]=1;
a1[2]=2;
a1[3]=3;
a1[4]=4;
a1[5]=5;
a1[6]=6;
a2=ArrayNew(1);
a2[1]="a";
a2[2]="b";
a2[3]="c";
a2[4]="d";
a3=ArrayInsertArrayAt(a1, a2, 2);
</cfscript>
<CFDUMP VAR="#a1#">
<CFDUMP VAR="#a2#">
<CFDUMP var="#a3#">

Parameters:

Name Description Required
a1 The first array. Yes
a2 The second array. Yes
pos The position to insert at. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Inserts an array at specified position in another array.
 * 
 * @param a1      The first array. 
 * @param a2      The second array. 
 * @param pos      The position to insert at.  
 * @return Returns an array. 
 * @author Craig Fisher (craig@altainteractive.com) 
 * @version 1, September 13, 2001 
 */

function ArrayInsertArrayAt(a1, a2, pos) {
    var aNew = ArrayNew(1);
    var len1 = "";
    var len2 = "";
    var i = 1;
    if ((NOT isArray(a1)) OR (NOT isArray(a2)) OR (NOT IsNumeric(pos)) OR (pos LT 1) OR (pos GT ArrayLen(a1) +1) )  {
        writeoutput("Error in <Code>ArrayInsertArrayAt()</code>! Correct usage: ArrayInsertArrayAt(<I>Array1</I>, <I>Array2</I>,
<I>position</I>) -- Inserts <I>Array2</I> at <I>position</I> in
<I>Array2</I>"
);
        return 0;
    }
    pos=int(pos);
    len1=ArrayLen(a1);
    len2=ArrayLen(a2);
    aNew=Duplicate(a1);
    if (pos IS NOT Len1 + 1) {
        for (i=0; i LT len2; i=i+1) ArrayInsertAt(aNew, pos + i, Duplicate(a2[i+1]));
    }
    else {
        for (i=1;i LTE len2;i=i+1) ArrayAppend(aNew, Duplicate(a2[i]));
    }    
    return aNew;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson