uf_value_exists_in_array() - check if array has value

Share this topic:



Link to this posting

Postby Ursego » 16 Apr 2013, 13:23

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(). You need to have the function uf_ds_from_array() prior to adding uf_value_exists_in_array().

Code: Select all
/**********************************************************************************************************************
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   Reports if the value exists in the array.
-----------------------------------------------------------------------------------------------------------------------
Arg:   as_val
      rs_arr[] (ref)
-----------------------------------------------------------------------------------------------------------------------
Thr:   n_ex - see http://forum.powerbuilder.us/viewtopic.php?f=2&t=1
**********************************************************************************************************************/
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."))
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

// Array is large - it's faster to deal with DataStore:
lds_temp = uf_ds_from_array(rs_arr[]) // http://forum.powerbuilder.us/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)
User avatar
Ursego
Site Admin
 
Posts: 112
Joined: 19 Feb 2013, 20:33

Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 2 guests


uf_value_exists_in_array() - check if array has value

Share this topic:


If you think that this site is not too bad, please LIKE it in Facebook. Thanks!





free counters

eXTReMe Tracker