uf_ds_from_array() to convert array to DataStore


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). A classic example - sorting a huge array. It's faster to convert it to a DS and call Sort().

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
/**********************************************************************************************************************
Dscr:         Gets STRING array and creates a DS, the only column of which (named "the_col") contains the array's values.
            After the values, previously converted to a DataStore by this function, have been processed, they can be
            easily converted back to an array by accessing the DataStore's property object.the_col.current:
      
            lds_temp = uf_ds_from_array(ls_arr[])
            ...massage data in DataStore...
            ls_arr[] = lds_temp.object.the_col.current
      
            For LONG array, use another overload.
***********************************************************************************************************************
Arg:         as_arr[]
***********************************************************************************************************************
Ret:         DataStore
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
string      ls_source
string      ls_error
string      ls_pb_version
DataStore   lds

ls_pb_version = this.uf_get_pb_version() // http://code.intfast.ca/viewtopic.php?f=4&t=91
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
/**********************************************************************************************************************
Dscr:         Gets LONG array and creates a DS, the only column of which (named 'the_col') contains the array's values.
            After the values, previously converted to a DataStore by this function, have been processed, they can be
            easily converted back to an array by accessing the DataStore's property object.the_col.current:
            
            lds_temp = uf_ds_from_array(ll_arr[])
            ...massage data in DataStore...
            ll_arr[] = lds_temp.object.the_col.current
            
            For STRING array, use another overload.
***********************************************************************************************************************
Arg:         al_arr[]
***********************************************************************************************************************
Ret:         DataStore
***********************************************************************************************************************
Developer:   Michael Zuskin -  http://linkedin.com/in/zuskin | http://code.intfast.ca/
**********************************************************************************************************************/
string      ls_source
string      ls_error
string      ls_pb_version
DataStore   lds

ls_pb_version = this.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

When 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 = gnv_util.uf_ds_from_array(ls_arr[])
...massage the data in lds_temp...
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: 143
Joined: 19 Feb 2013, 20:33



Ketones are a more high-octane fuel for your brain than glucose. Become a biohacker and upgrade yourself to version 2.0!



cron
Traffic Counter

eXTReMe Tracker