indexfind.inc -- finds first index of a value in an array

A forum for macro code snippets to be used in writing other macros. Post routines or .inc files here only, completed macros go to the Macro Depot.

Moderator: MacroQuest Developers

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

indexfind.inc -- finds first index of a value in an array

Post by Gumby » Fri Jan 30, 2004 2:24 pm

After beating my head against the wall error checking against arrays and aliasing this morning, tossing this up here so others can either use it, or at least see how it can be used without going through the same hassle I did. This has been checked as throughly as I'm capable of, but PM me if there's any issues.

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
Last edited by Gumby on Fri Jan 30, 2004 7:24 pm, edited 2 times in total.

ml2517
a grimling bloodguard
a grimling bloodguard
Posts: 1216
Joined: Wed Nov 12, 2003 1:12 am

Post by ml2517 » Fri Jan 30, 2004 2:47 pm

The only thing I think I'd change here is from a for next loop to just a normal loop. It'd never get stuck and should always return the right index.

* Removed my code to pretty up your post again. :)
Last edited by ml2517 on Fri Jan 30, 2004 7:37 pm, edited 1 time in total.

Gumby
a ghoul
a ghoul
Posts: 99
Joined: Sat Jan 24, 2004 5:27 pm

Post by Gumby » Fri Jan 30, 2004 6:29 pm

ml2517 wrote:The only thing I think I'd change here is from a for next loop to just a normal loop. It'd never get stuck and should always return the right index.
I didn't notice any hangs once I got the error checking down but you're right; coding in a standard /goto loop is probably better practice for this.

Thanks for the suggestion, will edit the main post (and switch the more useful routine I was about to post) to reflect the changes after I do a quick test.

G