CFLib.org – Common Function Library Project

FirstInFirstOut(array, valueToAdd)

Last updated May 13, 2003

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

 
Rated 1 time(s). Average Rating: 5.0

Description:
Deletes the first element in a given array, then inserts a new element at the end of the array, creating a first in first out effect.

Return Values:
Returns an array.

Example:

view plain print about
<cfset myArray = ArrayNew(1)>

<cfset myArray[1] = "First In"> 
<cfset myArray[2] = "Second In"> 
<cfset myArray[3] = "Third In"> 
<cfset myArray[4] = "Fourth In">

<cfoutput>
Before:<br>
<cfloop from="1" to="#ArrayLen(myArray)#" index="i">
   #myArray[i]#<br>
</cfloop>
</cfoutput>

<cfset myArray = FirstInFirstOut( myArray, "New Value" )>

<cfoutput>
<br>After:<br>
<cfloop from="1" to="#ArrayLen(myArray)#" index="i">
   #myArray[i]#<br>
</cfloop>
</cfoutput>

Parameters:

Name Description Required
array Array to modify. Yes
valueToAdd Value to add. Yes

Full UDF Source:

view plain print about
<cfscript>
/**
 * Removes the element at index one and inserts a new element at the highest index plus one.
 * 
 * @param array      Array to modify. (Required)
 * @param valueToAdd      Value to add. (Required)
 * @return Returns an array. 
 * @author Adrian Lynch (adrian.l@thoughtbubble.net) 
 * @version 1, May 13, 2003 
 */

function FirstInFirstOut( array, valueToAdd ) {

    // Delete element at index 1
    ArrayDeleteAt( array, 1 );
    
    // Add new element at last index plus one
    array[ArrayLen( array ) + 1] = valueToAdd;
    
    return array;
    
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson