Home Tutorials Forums Articles Blogs Movies Library Employment Press Buy templates

<< Prev 5 | Next 5

Array.addItemAt( item , index )

Array.prototype.addItemAt = function( item , index ){
        return( [ this.slice( 0 , index ) , item , this.slice( index , this.length ) ] );
}

var neto = [ 10 , 20 , 40 , 50 , 60 ];

trace( neto.addItemAt( 30 , 2 ) );

Posted by: Joćo Neto | website http://www.mugb.com.br
Array.addItemAt(index)
// This is a modification of João Neto's code to
// do the opposite of his function 'addItemAt'.
// This function removes an item from the array
// at the location specified. Be careful as the
// index for this function starts from 1 not 0
// i.e. if removeItemAt(1) is called the first
// element is removed, not the second.

Array.prototype.removeItemAt = function(index){
        if(index <= 1) return this.slice(index , this.length );
        else {
                var firstHalf:Array = this.slice( 0 , index );
                firstHalf.pop();
                var secondHalf:Array = this.slice( index , this.length );
                return [firstHalf,secondHalf];
        }
}


//////// EXAMPLE

var myArray:Array = Array("1","2","3","4","5","6","7","8","9","10");
trace(myArray); // 1,2,3,4,5,6,7,8,9,10
trace("**************");
trace(myArray.removeItemAt(4)); // 1,2,3,5,6,7,8,9,10

Posted by: Oussama (OZ) Mubarak | website http://ozarts.com
Array.bSearch (key, options) - search a sorted associative array using bisection algorithm
// Array.bSearch(key, options)
//
// Usage
//      my_array.bSearch( {role1: value1, role2: value2, ... , roleN: valueN} )
//      my_array.bSearch( {role1: value1, role2: value2, ... , roleN: valueN}, Array.DESCENDING | Array.NUMERIC | Array.CASEINSENSITIVE )
//
// Parameters
//      key       A list of field names (roles) and their associated values of any data type to search, grouped together into a block using curly braces {}.
//      options   Optional parameters that change the search behavior. Supported options are: Array.DESCENDING, Array.NUMERIC, Array.CASEINSENSITIVE.
//
// Returns
//      Integer value representing an array row (index) in which the search key was found, or -1 when not found.
//      A warning is displayed in output window via a trace(), when search takes more than theoretical maximum of log2(n) iterations.
//
// Description
//      Extension to class Array() to search for a key in a sorted associative array using very efficient bisection algorithm.
//      Array to search can use any data type since search key and array rows are converted to strings.
//      Array must be sorted prior to using this search method.
//
// References:
//      ActionScript Array.sortOn() for details on associative array sorting and sorting options.
//      Numerical Recipes section 3.4: bisection search algorithm, www.nr.com.
//
// Author: Ron Fredericks, Embedded Components, Inc.,  ronf@EmbeddedComponents.com
// Last updated: Dec. 28, 2004
// Tested environment: Flash player version 6, and 7 (both as1 and as2).
// Copyright:  no rights reserved, no liability covered, no warranty of use given or implied, no indemnity offered, support optional.

Array.prototype.bSearch = function (key, options) {
        this.sortDESCENDING       = (options & Array.DESCENDING) ? true : false;
        this.sortNUMERIC          = (options & Array.NUMERIC) ? true : false;
        this.sortCASEINSENSITIVE  = (options & Array.CASEINSENSITIVE) ? true : false;
        this.padRole              = String.fromCharCode(31); // Use this character to delimit fields in search key and array test row.
        var iL = 0, iU = this.length-1, iM = 0; // Initialize search boundry.
        var iterations = 0;                     // The bisection search algorithm iterates about log2(Array.length) times when searching a sorted array.
        
        while (iU-iL > 1) {
                iM = (iU+iL) >> 1;                  // Bisect array's search boundary until search key is found in the array row under test, or search fails boundary conditions.
                iterations++;
                this.getRow2Test(key, iM);          // Private function, defined below, to populate this.sKey and this.tKey.
                if ( (this.sKey >= this.tKey) != this.sortDESCENDING)
                iL=iM;
                else
                iU=iM;
        }
        if ( iterations > Math.round(Math.LOG2E*Math.log(this.length)) ) {
                trace( "Warning: search took too long! \n Details: " + iterations + " iterations of outer search loop were used, \n compared to theoretical maximum of log2(array size): " + Math.round(Math.LOG2E*Math.log(this.length)) );
                trace( " Perhaps this array has not been sorted correctly, or different options should be pased to this function." );
        }
        this.getRow2Test(key, (this.sortDESCENDING ? iU: iL))   // Return with Array index position when search found.
        if ( this.sKey == this.tKey )
        return (this.sortDESCENDING ? iU: iL);
        this.getRow2Test(key, 0)
        if (this.sKey == this.tKey)
        return 0;
        this.getRow2Test(key, this.length-1)
        if (this.sKey == this.tKey)
        return this.length-1;
        return -1;                         // Return -1, when search key not found.
};
// bSearch private function builds formatted search key (this.sKey) and test row (this.tKey)
Array.prototype.getRow2Test = function (key, row) {
        this.sKey="";                     // Search key.
        this.tKey="";                     // Test row.
        for (var role in key) {
                var iPadRole = Math.max( String(key[role]).length, String(this[row][role]).length ) + 1;
                if ( typeof key[role] == "number" && this.sortNUMERIC) {
                        // NUMERIC sorted number elements must pad to the right
                        for (var p = String(key[role]).length; p<iPadRole; p++)
                        this.sKey += this.padRole;
                        for (var p = String(this[row][role]).length; p<iPadRole; p++)
                        this.tKey += this.padRole;
                        this.sKey += String(key[role])
                        this.tKey += String(this[row][role])
                }
                else {
                        // STRING sorted elements must pad to the left
                        this.sKey += String(key[role])
                        this.tKey += String(this[row][role])
                        for (var p = String(key[role]).length; p<iPadRole; p++)
                        this.sKey += this.padRole;
                        for (var p = String(this[row][role]).length; p<iPadRole; p++)
                        this.tKey += this.padRole;
                }
        }
        if (this.sortCASEINSENSITIVE) {
                this.sKey = this.sKey.toUpperCase();
                this.tKey = this.tKey.toUpperCase();
        }
};

// example...
var playList = new Array();
playList.push( {slide: 2, clip: "_level0.instance3", cue: 4000} );
playList.push( {slide: 3, clip: "_level0.instance1", cue: 5000} );
playList.push( {slide: 1, clip: "_level0.instance1", cue: 1000} );
playList.push( {slide: 10, clip: "_level0.instance2", cue: 2000} );
playList.sortOn( ["clip", "slide"], Array.DESCENDING | Array.NUMERIC | Array.CASEINSENSITIVE)
var index = playList.bSearch( {clip: "_level0.instance1", slide: 1}, Array.DESCENDING | Array.NUMERIC | Array.CASEINSENSITIVE )
if (index)
trace("clip found, with cue time: " + playList[index].cue); // clip found, with cue time: 1000
else
trace("Oops, clip not found");

Posted by: Ron Fredericks | website http://www.embeddedcomponents.com/
Array.Contains
/*--------------------------------------------------------------------
Array.Contains(value)

A function to find if value is contained inside the array anywhere...

Author:
David B. (david@jmbsoftware.net)
Date:
July 13, 2003
Version:
0.1a
License:
As is! You assume all responsibility for this code.
Use as you see fit. Use at your own risk!

Inputs:
[value] = the value to find inside the array.
Returns:
[int] = the number of occurrence of that [value] inside the array.

Example:
var a;
var cnt;
a = new Array(1,2,3,3,3,4);
cnt = a.Contains(3);
trace(cnt);
------------------------------------------------------------------------*/
Array.prototype.Contains = function(value)
{
        var found = 0;
        var i = 0;
        
        for(i=0; i<this.length; i++)
        if( this[i] == value ) found++;
        
        return found;
}

Posted by: David B. | website http://jmbsoftware.net
Array.Contains_Mod
// many thanks to Array.Contains...
// here's a little modification
//returning the string where the searched was found and case sensitive

Array.prototype.Contains = function (value) {
        loc = new Array ();
        var found = 0;
        var i = 0;
        for (i = 0; i < this.length; i++) {
                if (this[i] == value or (this[i].indexOf (value) <> -1)) {
                        loc.push (this[i]);
                        found++;
                }
        }
        return found + ' [' + value + '\'s] found' + ' => ' + loc.toString ();
        delete loc;
};
choice = new Array ();
choice[0] = new Array ('ffo', 'mAry', 'envelope', 'mary', 'choA', 'a', 'Abraoa');
choice[1] = new Array (5, 43, 24, 33, 5563, 3346, 34, 3, 345, 3, 243);
choice[2] = new Array (354, 3, 678, 112, 74564, 1, 423, 26, 4678, 1245678, 44);
trace (choice[1].Contains ('5'));
trace (choice[0].Contains ('a'));
trace (choice[0].Contains ('A'));
trace (choice[0].Contains ('oA'));

Posted by: Mark Donovan | website http://www.gigtrak.com

<< Prev 5 | Next 5

Copyright 2000-2009 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.