Loading
  Password:
  Lost password?  
LogoutLOGOUT
Join as a MemberJoin as a Member
 
FlashMove Live Support



Go Back   FlashMove Forums > Flash Coding > Archives > Flash MX ActionScript

 
 
Thread Tools Search this Thread Display Modes
  #1  
Old 11-05-2002, 07:21 PM
bouet bouet is offline
Registered User
 
Join Date: Nov 2002
Posts: 1
bouet is on a distinguished road
An array / index issue

Hi,

Does someone know an array method (or something else) that returns the index of an element of this array.

For instance :

array[0]="joe";
array[1]="nono";
array[2]="dudul";
array[3]="titi";

If I ask to this "method" the index of the element "joe", I expect it to return 0.

If I ask to this "method" the index of the element "dudul", I expect it to return 2.

It sounds easy... did not find the method through the actionscript dictionnary though...

Thanks for your help
  #2  
Old 11-05-2002, 09:20 PM
niq niq is offline
Registered User
 
Join Date: Sep 2002
Posts: 267
niq is on a distinguished road
try something like this
Actionscript:
function getindex (name){
    for (i=0;i<array.length;++i){
    if (name==array[i]){
        nameindex=i;
    }
}

getindex("dudul");
dunno how to break the loop when u got the index but as long as the array isn't too long that doesn't matter. hope the thing worx.
  #3  
Old 11-06-2002, 02:51 AM
MixMatch's Avatar
MixMatch MixMatch is offline

Hyper Moderator
 
Join Date: Aug 2001
Location: Heidelberg, Germany
Posts: 1,789
MixMatch is on a distinguished road
Heres an alternative:
Actionscript:
// every array you create will now have this function
Array.prototype.getIndex = function(searchWord) {
	var indexArray = new Array();
	for (var counter in this) {
		if (this[counter] == searchWord) {
			indexArray.push(counter);
		}
	}
	return indexArray;
	};
//you must define your array as new Array() for it to work
myArray = new Array();
myArray[0] = "joe";
myArray[1] = "nono";
myArray[2] = "dudul";
myArray[3] = "titi";
myArray[4] = "dudul";
//the following lines are alternate methods of defining the array that also work
//myArray = new Array("joe","nono","dudul","titi","dudul");
//myArray = ["joe","nono","dudul","titi","dudul"];
//will give you the an array with the index of every value that is "dudul"
myResults = myArray.getIndex("dudul");
trace (myResults);

This can be extended to give all array indices that contain the searchWord...
Actionscript:
Array.prototype.getIndex = function(searchWord) {
	var indexArray = new Array();
	for (var counter in this) {
		if (this[counter].toString().indexOf(searchWord)!= -1) {
			indexArray.push(counter);
		}
	}
	return indexArray;
	};
myArray = new Array();
myArray[0] = "joe";
myArray[1] = "nono";
myArray[2] = "dudul is nice";
myArray[3] = "titi";
myArray[4] = "dudul is crazy";
myResults = myArray.getIndex("dudul");
trace (myResults);

The for (var watever in myObject) code is nice because its simpler than the alternative for(var i = 0; i<myArray.length;i++). It is also very nice in cases where you define the values of your array without using simple numbers (e.g. myArray["bob"]) because these values are hard to access through the conventional for loop. Using a for(..in..) loop, however, it access EVERY value in the array. Unfortunately, this can also be a negative. In the case of my code, myArray.getIndex is found also, even though it is not really part of the content of my array(which sux). that is why I had to add the .toString part to the second code example I gave...
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Play Coordinated objects in Array heimer22 Flash MX 2 07-07-2004 10:55 PM
Resolved: display contents of an array Lelboy Flash MX 2 05-06-2004 03:46 PM
trouble sorting dynamic Array RacerX Flash MX ActionScript 2 12-05-2003 09:24 PM
Problems reading from an Array dogzi Flash 5 Actionscript 4 07-09-2001 03:59 PM



All times are GMT. The time now is 09:59 PM.




Powered by vBulletin version 3.5