Home AMX User Forum AMX General Discussion

Find_String for arrays

Does anybody know if there is a function similar to Find_String but for integer arrays? It's just to know if a number it's already in an array.

Thanks

Comments

  • mpullinmpullin Posts: 949
    I know of no such function built in.
    DEFINE_FUNCTION INTEGER isinarray(INTEGER haystack[], INTEGER needle){
      // returns first occurrence of needle in haystack, 0 if needle not in haystack.
      STACK_VAR INTEGER i;
      STACK_VAR INTEGER max;
      i = 1;
      max = LENGTH_ARRAY(haystack);
      while(i <= max){
        if(haystack[i] == needle) return i;
        i++;
      }
      return 0;
    }
    
  • AMXJeffAMXJeff Posts: 450
    mpullin wrote: »
    I know of no such function built in.
    DEFINE_FUNCTION INTEGER isinarray(INTEGER haystack[], INTEGER needle){
      // returns first occurrence of needle in haystack, 0 if needle not in haystack.
      STACK_VAR INTEGER i;
      STACK_VAR INTEGER max;
      i = 1;
      max = LENGTH_ARRAY(haystack);
      while(i <= max){
        if(haystack[i] == needle) return i;
        i++;
      }
      return 0;
    }
    


    So you do not like FOR loops???? Old School????
    DEFINE_FUNCTION INTEGER isinarray(INTEGER haystack[], INTEGER needle){
      // returns first occurrence of needle in haystack, 0 if needle not in haystack.
      STACK_VAR INTEGER i;
    
      for(i = 1; i <= MAX_LENGTH_ARRAY(haystack); i++) {
        if(haystack[i] == needle) 
          return i;
      }
      return 0;
    }
    
  • mpullinmpullin Posts: 949
    AMXJeff wrote: »
    So you do not like FOR loops???? Old School????
    lol, just realized that. No reason not to use a for loop there. Also, having to call MAX_LENGTH_ARRAY() every iteration is probably not important unless haystack has billions and billions of hay in it, not likely the case in anyone's home control system. But still I'd rather not.
  • viningvining Posts: 4,368
    How about a compromise?
    DEFINE_FUNCTION INTEGER isinarray(INTEGER haystack[], INTEGER needle){
      // returns first occurrence of needle in haystack, 0 if needle not in haystack.
      STACK_VAR INTEGER i;
      STACK_VAR INTEGER max;
    
      max = MAX_LENGTH_ARRAY(haystack);
      for(i = 1; i <= max; i++) {
        if(haystack[i] == needle) 
          return i;
      }
      return 0;
    }
    
  • ericmedleyericmedley Posts: 4,177
    I suppose technically the FIND_STRING allows you to also pick the staring point of the string to look for things. EX:
    FIND_STRING(array,'some srting',6) // start looking for the stirng at cell 6..
    



    So,
    if(staring_point<max)
    {
      for(i=staring_point,i<=max;i++)
      etc...
    }
    

    I know, I know... I better go find something useful to do... :D
  • mpullinmpullin Posts: 949
    FIND_STRING

    Isn't that rather unorthodox though, using a function that was designed to find a char in a char array, to find an integer in an integer array? :-|

    Sure enough, FIND_STRING(arrADA_VOLUME,"30",1) does not compile.

    ERROR: C:\Programming\AMX\???.axs(975): C10585: Dimension mismatch: [0] vs. [1]
    ERROR: C:\Programming\AMX\???.axs(975): C10555: Type mismatch in call for parameter [A]
    (where arrADA_VOLUME is an integer array of size 13)
  • Thanks

    Thank you guys for the replies. I thought that maybe Netlinx would have a specific function to do that :D

    I can look now for mu needle inside my haystack :P
  • a_riot42a_riot42 Posts: 1,624
    Is the integer array sorted? If so then a binary search is easy and fast: O(logn).

    If you know the maximum amount of possible integers and they are unique then you could put the integer in the array at the index of the integer. This would then require only one access and would run in O(1) time. It would use more memory but often that is a suitable trade off. For instance if you had an integer array of channel numbers in use, since they are guaranteed to be unique, you could define an array of 4000, and then store the number 1 at index 1, 2 at index 2 etc. If there was no integer at that location then its value would be 0 so you know that integer isn't in the array. Won't work for negative numbers though unless you come up with a clever scheme. Every time you insert a number into the array it goes into its correct element and you only need to do this to find out if the integer is in the array:
    if (array[someInteger] == someInteger)
    {
      // someInteger is in the array
    }
    else
    {
      // someInteger is not in the array
    }
    


    Not sure what the application is but if you tailor the way you assign integers to array elements you can find them quite a bit quicker. A hash would be perfect for what you need but you would have to write your own as there is no built in hashing function.
    Paul
Sign In or Register to comment.