Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 07-09-2004, 04:49 PM   #1
brendanb
Registered User
 
Join Date: Jun 2003
Location: Cincinnati
Posts: 9
Default finding the position of an array index

i was wondering if you have an array value, can you find what number it is in the array's order. For example,

letters=new Array("a","b","c","d");
cnumber=letters.position["c"]
trace(cnumber);

so that cnumber=2. How would I go about doing something like this?

-Brendan
brendanb is offline   Reply With Quote
Old 07-09-2004, 05:50 PM   #2
tg
done
 
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
Default

this code was from our library section. its by Roland Levy.
Code:
Array.prototype.getPos = function(item){
        for(i=0; i<this.length; ++i) {
                if (this[i] == item) { return i; }
        }
        return null;
};
__________________
tg
---
what the hell was i thinking?
tg is offline   Reply With Quote
Old 07-09-2004, 05:51 PM   #3
tg
done
 
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
Default

useage would be:
trace(letters.getPos("c"));
__________________
tg
---
what the hell was i thinking?
tg is offline   Reply With Quote
Old 03-30-2010, 07:09 PM   #4
BluegillMedia
pancake batter
 
BluegillMedia's Avatar
 
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
Default

This doesn't work for me in CS2, neither did indexOf. I am sure it is easy to just have it tell you what position a string is within an array, but none of these seem to work. Any ideas?

Quote:
Originally Posted by brendanb View Post
i was wondering if you have an array value, can you find what number it is in the array's order. For example,

letters=new Array("a","b","c","d");
cnumber=letters.position["c"]
trace(cnumber);

so that cnumber=2. How would I go about doing something like this?

-Brendan
__________________
Hey! I like potatos!
BluegillMedia is offline   Reply With Quote
Old 03-30-2010, 07:22 PM   #5
CyanBlue
Super Moderator
 
CyanBlue's Avatar
 
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
Default

So, you are saying that the above code does not work on CS2/AS2???

What do you get when you create a new FLA, paste the code and test movie???
ActionScript Code:
// Array.prototype.getPos = function (item) {     for (i = 0; i < this.length; ++i)     {         if (this[i] == item)         {             return i;         }     }     return null; }; letters = new Array("a", "b", "c", "d"); trace(letters.getPos("c"));
__________________
CyanBlue / Jason Je / Macromedia Certified Flash Developer & Designer
http://CyanBlue.FlashVacuum.com
http://www.FlashVacuum.com
http://tutorials.FlashVacuum.com

Do NOT PM, Email or Call me... Your question belongs right in this forum...
CyanBlue is offline   Reply With Quote
Old 03-30-2010, 07:54 PM   #6
tacos
Grrrrrrrrreat!
 
tacos's Avatar
 
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
Default

Works fine with MX2004, as I guess it would with FlashMX.
__________________
WHO I WAS...
BANDS OF THE 80's
tacos is offline   Reply With Quote
Old 03-30-2010, 07:55 PM   #7
BluegillMedia
pancake batter
 
BluegillMedia's Avatar
 
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
Default

sorry i meant to say AS2
__________________
Hey! I like potatos!
BluegillMedia is offline   Reply With Quote
Old 03-30-2010, 07:56 PM   #8
BluegillMedia
pancake batter
 
BluegillMedia's Avatar
 
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
Default

I ended up having to use this (which i found on here, but added the 'whicharray' being passed):

Code:
_root.findPos = function(value, whicharray) {
	var pos;
	for (var i = 0; i < ArrayName.length; i++) {
		if (ArrayName[i].toUpperCase() == value.toUpperCase()) {
			pos = i;
			break;
		} else {
			pos = false;
		}
	}
	return pos;
}
__________________
Hey! I like potatos!

Last edited by BluegillMedia; 03-30-2010 at 07:57 PM. Reason: typo
BluegillMedia is offline   Reply With Quote
Old 03-30-2010, 08:15 PM   #9
tacos
Grrrrrrrrreat!
 
tacos's Avatar
 
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
Default

MX & MX2004 are only AS2.0.
__________________
WHO I WAS...
BANDS OF THE 80's
tacos is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT. The time now is 12:28 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.