07-09-2004, 04:49 PM
|
#1
|
|
Registered User
Join Date: Jun 2003
Location: Cincinnati
Posts: 9
|
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
|
|
|
07-09-2004, 05:50 PM
|
#2
|
|
done
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
|
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?
|
|
|
07-09-2004, 05:51 PM
|
#3
|
|
done
Join Date: Jun 2001
Location: portland, or
Posts: 8,106
|
useage would be:
trace(letters.getPos("c"));
__________________
tg
---
what the hell was i thinking?
|
|
|
03-30-2010, 07:09 PM
|
#4
|
|
pancake batter
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
|
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
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!
|
|
|
03-30-2010, 07:22 PM
|
#5
|
|
Super Moderator
Join Date: Jan 2002
Location: Centreville, VA
Posts: 26,666
|
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"));
|
|
|
03-30-2010, 07:54 PM
|
#6
|
|
Grrrrrrrrreat!
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
|
Works fine with MX2004, as I guess it would with FlashMX.
|
|
|
03-30-2010, 07:55 PM
|
#7
|
|
pancake batter
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
|
sorry i meant to say AS2
__________________
Hey! I like potatos!
|
|
|
03-30-2010, 07:56 PM
|
#8
|
|
pancake batter
Join Date: Dec 2003
Location: Calgary Canada
Posts: 260
|
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
|
|
|
03-30-2010, 08:15 PM
|
#9
|
|
Grrrrrrrrreat!
Join Date: Apr 2007
Location: Montréal, Québec
Posts: 5,473
|
MX & MX2004 are only AS2.0.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT. The time now is 03:38 PM.
///
|
|