Flasher Archive

[Previous] [Next] - [Index] [Thread Index] - [Previous in Thread] [Next in Thread]


Subject: RE: [flasher] array.indexOf
From: Paul Willoughby
Date: Mon, 29 Jan 2001 16:16:43 -0000

Hi Scott,

If I understand what you want to do correctly, I think you need to convert
the Array to a string to do this (myArray.toString). You can then use
myString.IndexOf to find the first occurrence of a particular character, or
myString.lastIndexOf to find the last (look under String in the AS
dictionary).

Or are you looking for an entire array element, like: If(myArray[2]==
"foo")? If that's the case then the only way to find an element that matches
is to loop through the whole array until you find what you want.

*warning* it's been reported that the string.IndexOf, as with the other new
string functions, is very slow if you have a large string to deal with.
Branden Hall of figleaf came up with this new Indexof function that is
faster that the built in one (i don't really understand it, so if you have
any questions I recommend asking them on the Flashcoders list - join at
http://chattyfig.figleaf.com/):

function indexOf2(sub, i){
if (i == null){
i = 0;
}
var found = false;
var size = sub.length;
var max = this.length+1;
while (!found && (i+size)<=max){
if (substring(this, i, size) == sub){
found = true;
}else{
++i;
}
}
if (found){
return (i-1);
}else{
return -1;
}
}
String.prototype.indexOf2 = indexOf2;


for(i=0;i<199;++i){
foo+="aaaaaaaaaa";
}
foo+="bbbbbbbbbb";
trace("Performing indexOf against a "+foo.length+" character string (worst
case)");
trace("--------------------------------------------------------------");
trace("Testing internal indexOf:");
start = getTimer();
trace("Found at position: "+foo.indexOf("bbbbbbbbbb"));
time1 = ((getTimer() - start)/1000);
trace("Took: "+time1+" seconds");
trace("\nTesting custom indexOf:");
start = getTimer();
trace("Found at position: "+foo.indexOf2("bbbbbbbbbb"));
time2 = ((getTimer() - start)/1000);
trace("Took: "+time2+" seconds");
trace("\nCustom code is "+(time1/time2)+" times faster in this case");


hth

paul

> -----Original Message-----
> From: Scott Rouse [sfrouseatevermore88 [dot] com (mailto:sfrouseatevermore88 [dot] com)]
> Sent: Friday, January 26, 2001 5:02 PM
> To: flasher from chinwag
> Subject: [flasher] [flasher]array.indexOf
>
>
> I have searched through the help for any piece of code that
> will allow me to
> find the index of a specific string that exists within an
> array. I have
> found nothing. I finally created a work-around that loops
> through the whole
> array until it finds a match...but that seems just wacked to
> me. Director
> does this and it seems to me that this would be a basic function and a
> virtue of utilizing an array (anytime you splice the array,
> anything after
> the splice has a new index number). If I missed something
> could someone
> point it out...and if I didn't would anyone want the goofy
> work-around?
>
> Thanks
> Scott Rouse



[Previous] [Next] - [Index] [Thread Index] - [Next in Thread] [Previous in Thread]