PDA

View Full Version : Array question...



zander
11-18-2003, 09:22 AM
Maybe I'm overlooking the obvious... Is there a simple way to tell if an item already exists in an array? (Like indexOf() or getPos() or something...)

I'm gathering a list of point IDs and I don't want duplicate values. The following subroutine does the trick, however if I'm dealing with lots of points, it would get really slow to loop through all the values and compare them...

Thanks!



subExistsInArray: val, arr
{
// This subroutine steps through an array and
// return 1 or 0 depending on if the value is
// found in the array.
booExists = 0;
for (k = 1; k <= arr.count(); k++)
{
if (arr[k] == val)
{
booExists = 1;
break;
}
}
return booExists;
}

faulknermano
11-18-2003, 09:35 AM
try contains(). it's in the 2.6 or 2.6.1 release notes. must read. :)

zander
11-18-2003, 10:22 AM
Damn. Well, that would explain why I couldn't find it. Thank you very much faulknermano.

I'm not sure what version of Lscript I'm running right now, but I'm pretty sure I haven't updated it yet.

I'm off to download it right now. Thanks again.