EDIT/Update: Incorporated ML's suggestion, also tossed a few of my local comments and basic end routine validity check since it was written for public consumption and may be useful to another if they decide to use the function. Updates went through the same test routine used for the previous version.
G
Code: Select all
| -- IndexFind.inc
| Code by Gumby 1/30/04
|
Sub IndexFind(ArrayName,SearchValue)
| IndexFind searches through an array for a value and will find the
| first matching value and return its index.
|
| Return Codes:
|
| * $return>=0 SearchValue found at this index in the array ArrayName
| * $return==-1 SearchValue was not found in the array ArrayName
| * $return==-2 No parameters were passed to IndexFind
| * $return==-3 A valid array was not passed to IndexFind
| * $return==-4 An uninitialized array was passed to IndexFind
| * $return==-5 SearchValue was not passed to IndexFind
|
|
| CAVEATS: Because of the aliasing error checking a bogus array is
| frankly odd. The creative "solution" I'm using has one
| flaw: Do not set the 0 index element of the array you're
| passing to IndexFind to be '(0)'. I have no idea why
| anyone would have an array with this as an element in MQ,
| let alone the first one... but if you do, IndexFind will
| will erroneously return -3 for the valid array. If someone
| figures out a better way to check for array validity, please
| PM me on the forums.
|
|
| USAGE: This sub is called with the following syntax:
| /call IndexFind <ArrayName> "<SearchValue>"
|
| *** NOTE: This sub makes use of aliasing (@@ArrayName), for ArrayName *only*
| pass the name of the array, do not attempt to pass the actual array itself.
/declare IndexFindCounter local
/declare FoundIndex local
/declare LocalDebugFlag local
| LocalDebugFlag: Basic error checking debug, 1 to turn on, 0 to turn off
/varset LocalDebugFlag 0
/varset FoundIndex -1
| Check that the Array (name) was passed
/if "$defined(ArrayName)"=="FALSE" {
/return -2
}
| Check that a valid array was passed
/if "$right(3,"@@ArrayName(0)")"=="(0)" {
/return -3
}
| Check that an initialized array was passed
/if "@@ArrayName(0)"=="UNDEFINED-ARRAY-ELEMENT" {
/return -4
}
| Check that a SearchValue was passed
/if "$defined(SearchValue)"=="FALSE" {
/return -5
}
/varset IndexFindCounter 0
:StartIndexFind
/if "@@ArrayName(@IndexFindCounter)"=="UNDEFINED-ARRAY-ELEMENT" {
/goto :EndIndexFind
}
/if "@@ArrayName(@IndexFindCounter)"=="@SearchValue" {
/varset FoundIndex @IndexFindCounter
/goto :EndIndexFind
}
/varadd IndexFindCounter 1
/goto :StartIndexFind
:EndIndexFind
| Debug Code if you turn it on: simply echo's out if found (and index) or not.
/if n @LocalDebugFlag==1 {
/if n @FoundIndex>=0 {
/echo I found @SearchValue at @FoundIndex in @ArrayName
}
/if n @FoundIndex==-1 {
/echo I did not find @SearchValue in @ArrayName
}
}
/return @FoundIndex

