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).

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.
            Another overload does the same for LONG.
***********************************************************************************************************************
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.
            Another overload does the same for STRING.
***********************************************************************************************************************
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

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 = gnv_util.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: 118
Joined: 19 Feb 2013, 20:33



IF you want to ((lose weight) OR (have unbelievable (brain function AND mental clarity))) THEN click:




free counters

eXTReMe Tracker