uf_ds_from_array() to convert array to DataStore

Share this topic:



Link to this posting

Postby Ursego » 16 Apr 2013, 12:43

uf_ds_from_array() gets an array and creates a DataStore, the only column of which (named "the_col") contains the array's values. It is useful in situations when you have an array, but it's faster or simply more convenient to process its values when they are in a DataStore (which has fast built-in functions to avoid looping on the array).

First of all, add to your util NVO the function uf_get_pb_version(). After that, add uf_ds_from_array(). It has 2 overloads - for string and long data types:

String:

Code: Select all
/*----------------------------------------------------------------------------------------------------------------------
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   Gets string array and creates a DS, the only column of which (named "the_col") contains the array's values.
------------------------------------------------------------------------------------------------------------------------
Arg:   as_arr[] - string array to convert to DataStore
------------------------------------------------------------------------------------------------------------------------
Ret:   DataStore
----------------------------------------------------------------------------------------------------------------------*/
string      ls_source
string      ls_error
string      ls_pb_version
DataStore   lds

ls_pb_version = uf_get_pb_version()
ls_source = 'release ' + ls_pb_version + '; datawindow() table(column=(type=char(10000) name=the_col dbname="the_col") )'

lds = create DataStore
lds.create(ls_source, ls_error)

if UpperBound(as_arr) > 0 then
   lds.object.the_col.current = as_arr
end if

return lds

Long:

Code: Select all
/*----------------------------------------------------------------------------------------------------------------------
Acc:   public
-----------------------------------------------------------------------------------------------------------------------
Dscr:   Gets decimal array and creates a DS, the only column of which (named 'the_col') contains the array's values.
------------------------------------------------------------------------------------------------------------------------
Arg:   al_arr[] - long array to convert to DataStore
------------------------------------------------------------------------------------------------------------------------
Ret:   DataStore
----------------------------------------------------------------------------------------------------------------------*/
string      ls_source
string      ls_error
string      ls_pb_version
DataStore   lds

ls_pb_version = uf_get_pb_version()
ls_source = 'release ' + ls_pb_version + '; datawindow() table(column=(type=decimal(0) name=the_col dbname="the_col") )'

lds = create DataStore
lds.create(ls_source, ls_error)

if UpperBound(al_arr) > 0 then
   lds.object.the_col.current = al_arr
end if

return lds

After the values, previously converted to a DataStore by uf_ds_from_array(), have been processed , they can be easily converted back to an array by accessing the DataStore's property object.the_col.current:

Code: Select all
lds_temp = uf_ds_from_array(ls_arr[])
...massage data in DataStore...
ls_arr[] = lds_temp.object.the_col.current

So, there is no need in the function uf_array_from_ds(). :)
User avatar
Ursego
Site Admin
 
Posts: 111
Joined: 19 Feb 2013, 20:33

Return to Tips and Tricks

Who is online

Users browsing this forum: No registered users and 1 guest


uf_ds_from_array() to convert array to DataStore

Share this topic:


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





cron
free counters

eXTReMe Tracker