Pages

Wednesday, January 18, 2012

Python Index function for Mel

Hello,
I thought this short script I wrote might come in handy.

Thanks
Nathaniel




/////give me the first index where input value matches input search array (returns int array)
//it behaves alot like pythons list.index(value) function, where search array acts as list
//it returns empty array if no index match found
/*
needs arguments
value string
searchArray string array
*/
global proc int[]
naIndex(string $value, string $searchArray[] )
{
int $result[];

//loop search array
for($i=0; $i < size($searchArray); $i++){
//if match found store index and break out of loop
int $matchValue = strcmp($value, $searchArray[$i]);
if( $matchValue == 0 )
{
$result[0] = $i;
break;
}
}

return $result;
}