uf_value_exists_in_array() - check if array has value


Link to this posting

The function reports if the value exists in the array. If the array is small (100 elements or less) then the function simply loops on it, otherwise it creates a DataStore and uses Find(). It has 2 overloads - for string and long data types:

String:

Code: Select all
/**********************************************************************************************************************
Dscr:         Reports if the value exists in a STRING array. If the array is small (100 elements or less) then the function
            simply loops on it; otherwise, it creates a DataStore and uses Find().
            Another overload does the same for a LONG array.
***********************************************************************************************************************
Arg:         as_val
            rs_arr[] (ref)
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
long         ll_upper_bound
long         ll_row
DataStore   lds_temp

if uf_empty(as_val) then
   f_throw(PopulateError(0, "Arg as_val is empty.")) // f_throw(): http://code.intfast.ca/viewtopic.php?f=2&t=1
end if

// If array is small then we can LOOP:
ll_upper_bound = UpperBound(rs_arr[])
if ll_upper_bound <= 100 then
   for ll_row = 1 to ll_upper_bound
      if rs_arr[ll_row] = as_val then
         return true
      end if
   next
   return false
end if

// The array is large - it's faster to use DataStore search:
lds_temp = uf_ds_from_array(rs_arr[]) // uf_ds_from_array(): http://code.intfast.ca/viewtopic.php?f=4&t=92
lds_temp.object.val.current = rs_arr[]
ll_row = lds_temp.Find("the_val=" + as_val, 1, ll_upper_bound)
destroy lds_temp

return (ll_row > 0)


Long:

Code: Select all
/**********************************************************************************************************************
Dscr:         Reports if the value exists in a LONG array. If the array is small (100 elements or less) then the function
            simply loops on it; otherwise, it creates a DataStore and uses Find().
            Another overload does the same for a STRING array.
***********************************************************************************************************************
Arg:         al_val
            rl_arr[] (ref)
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
long         ll_upper_bound
long         ll_row
DataStore   lds_temp

if uf_empty(al_val) then
   f_throw(PopulateError(0, "Arg al_val is empty.")) // f_throw(): http://code.intfast.ca/viewtopic.php?f=2&t=1
end if

// If array is small then we can LOOP:
ll_upper_bound = UpperBound(rl_arr[])
if ll_upper_bound <= 100 then
   for ll_row = 1 to ll_upper_bound
      if rl_arr[ll_row] = al_val then
         return true
      end if
   next
   return false
end if

// The array is large - it's faster to use DataStore search:
lds_temp = uf_ds_from_array(rl_arr[]) // uf_ds_from_array(): http://code.intfast.ca/viewtopic.php?f=4&t=92
lds_temp.object.val.current = rl_arr[]
ll_row = lds_temp.Find("the_val=" + al_val, 1, ll_upper_bound)
destroy lds_temp

return (ll_row > 0)