Programming question about storing Rexx variables in name/value pair spaces

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Programming question about storing Rexx variables in name/value pair spaces

Kermit Kiser
This question and example is taken from the Android environment but has wider application since scripting environments like
jEdit or JSR223 also use name/value spaces to store and pass data items. IIRC the JSR223 environment only stores and passes string names and values,
while jEdit and Android store simple data types such as integers, booleans, and strings. Android provides a "preferences" interface to
store name/value pairs to a flat XML file. In order to store an indexed Rexx variable using this mechanism, I wrote the code you can see in the program
excerpt below. The Rexx base value is stored by name in this format: value[index1][index2]...[indexn], where each index is attached with delimiters "[" and "]".
Names are then generated to store each indexed value under in this format: name[indexn]

This works OK as seen in the log dump below with one limitation: values (and index names) cannot include the characters "[" and "]".

Now here is the question: Is there a better technique which does not restrict the string values and which does not add significant processing overhead?
(In Android programming, excess CPU usage = battery drain and poor device response.)

-- Kermit
PS: I hope this format comes through without too much scrambling. If not I will try putting it in a web page.


03-12 19:28:12.022: INFO/1tta(3043): setprefs x=?
03-12 19:28:12.030: INFO/1tta(3043): setpref x=?
03-12 19:28:12.030: INFO/1tta(3043): setpref x[a]=a
03-12 19:28:12.030: INFO/1tta(3043): setpref x[a][c]=c
03-12 19:28:12.030: INFO/1tta(3043): setpref x[a][c][d]=d
03-12 19:28:12.038: INFO/1tta(3043): setpref x[a][b]=b

03-12 19:28:12.061: INFO/1tta(3043): getprefs name=x
03-12 19:28:12.069: INFO/1tta(3043): getprefs x=?
03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a]=a
03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][c]=c
03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][c][d]=d
03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][b]=b

03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y=?
03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a]=a
03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][c]=c
03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][c][d]=d
03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][b]=b

               
  method testrexxprefs
         
                        x="?"
                        x["a"]="a"
                        x["a","b"]="b"
                        x["a","c"]="c"
                        x["a","c","d"]="d"
                       
                        setprefs("x",x) -- store the Rexx variable "x" in a file
                       
                        y=getprefs("x") -- read the Rexx variable from file "x" into Rexx variable "y"
                       
                        dumpprefs("y",y) -- dump the Rexx variable "y" to the log

------------------------------------------------------------------------------------------------------------------------
        method setprefs(n=Rexx,v=Rexx) -- this method allocates a preference file and stores a Rexx variable in it

          Log.i("1tta", "setprefs" n"="v)
                prefs = getSharedPreferences(n.toString, 0) -- open a preference file
                editor = prefs.edit() -- init a preference change buffer
                setpref(n,v,editor) -- call recursive store of Rexx values and indexes

           editor.commit -- commit the preference changes to the file

        method setpref(n=Rexx,v=Rexx,ed=Editor) -- this method stores all indexed values by calling itself recursively
          Log.i("1tta", "setpref" n"="v)
                       
                        tv=v -- save non-indexed value
                        loop i over v -- check each index
                                tv = tv'['i']' -- save the index
                                setpref(n'['i']',v[i],ed) -- save the value
                                end
                               
        ed.putString(n.toString,tv.toString) -- store the base value and it's delimited indexes

-------------------------------------------------------------------------------------------------------------------------
        method getprefs(n=Rexx) returns Rexx -- this method locates a preference file and reads the Rexx variable stored in it

          Log.i("1tta", "getprefs name="n)
                prefs = getSharedPreferences(n.toString, 0) -- open a preference file
                return getprefs(n,prefs) -- Build a Rexx indexed variable from the file

        method getprefs(n=Rexx,p=SharedPreferences) returns Rexx -- this method reads all indexed values by calling itself recursively
          tv=p.getString(n,null) -- load base Rexx value and indexes
          parse tv initval '[' indexes -- check for index delimiter
          v=initval -- save base value
          Log.i("1tta", "getprefs" n'='v)
         
          if indexes\="" then do -- we have indexed sub values
            indexes='['indexes -- for parse loop
           
            loop while indexes\="" -- loop for each index
              parse indexes '[' nextindex ']' indexes -- get index name
              v[nextindex]=getprefs(n'['nextindex']',p)    --      restore indexed value
              end
             
            end
           
          return v -- return indexed Rexx variable
           
 ------------------------------------------------------------------------------------------------------------------------
        method dumpprefs(n=Rexx,v=Rexx) -- this method dumps an indexed Rexx variable to the log by calling itself
               
          Log.i("1tta", "dumpprefs" n"="v)
                       
                        loop i over v
                                dumpprefs(n'['i']',v[i])
                                end
-------------------------------------------------------------------------------------------------------------------------
           

_______________________________________________
Ibm-netrexx mailing list
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Programming question about storing Rexx variables in name/value pair spaces

Thomas.Schneider.Wien
Hi Kermit,
   you might want to use ReySymb.symbol(...) and ReySymb.value(...)
(formerly called RexxSymb.symbol and RexxSymb.value.

   What I did, in the current release, is an also an 'Environ' method to
setup the Environment.

When interested, give me a mail, and I could send you the source for
your usage.
Tom.
================================================================.

Kermit Kiser schrieb:

> This question and example is taken from the Android environment but
> has wider application since scripting environments like jEdit or
> JSR223 also use name/value spaces to store and pass data items. IIRC
> the JSR223 environment only stores and passes string names and values,
> while jEdit and Android store simple data types such as integers,
> booleans, and strings. Android provides a "preferences" interface to
> store name/value pairs to a flat XML file. In order to store an
> indexed Rexx variable using this mechanism, I wrote the code you can
> see in the program
> excerpt below. The Rexx base value is stored by name in this format:
> value[index1][index2]...[indexn], where each index is attached with
> delimiters "[" and "]". Names are then generated to store each indexed
> value under in this format: name[indexn]
>
> This works OK as seen in the log dump below with one limitation:
> values (and index names) cannot include the characters "[" and "]".
>
> Now here is the question: Is there a better technique which does not
> restrict the string values and which does not add significant
> processing overhead?
> (In Android programming, excess CPU usage = battery drain and poor
> device response.)
> -- Kermit
> PS: I hope this format comes through without too much scrambling. If
> not I will try putting it in a web page.
>
>
> 03-12 19:28:12.022: INFO/1tta(3043): setprefs x=?
> 03-12 19:28:12.030: INFO/1tta(3043): setpref x=?
> 03-12 19:28:12.030: INFO/1tta(3043): setpref x[a]=a
> 03-12 19:28:12.030: INFO/1tta(3043): setpref x[a][c]=c
> 03-12 19:28:12.030: INFO/1tta(3043): setpref x[a][c][d]=d
> 03-12 19:28:12.038: INFO/1tta(3043): setpref x[a][b]=b
>
> 03-12 19:28:12.061: INFO/1tta(3043): getprefs name=x
> 03-12 19:28:12.069: INFO/1tta(3043): getprefs x=?
> 03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a]=a
> 03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][c]=c
> 03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][c][d]=d
> 03-12 19:28:12.069: INFO/1tta(3043): getprefs x[a][b]=b
>
> 03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y=?
> 03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a]=a
> 03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][c]=c
> 03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][c][d]=d
> 03-12 19:28:12.069: INFO/1tta(3043): dumpprefs y[a][b]=b
>
>                 method testrexxprefs
>                         x="?"
>             x["a"]="a"
>             x["a","b"]="b"
>             x["a","c"]="c"
>             x["a","c","d"]="d"
>            
>             setprefs("x",x)            --    store the Rexx variable
> "x" in a file
>            
>             y=getprefs("x")            --    read the Rexx variable
> from file "x" into Rexx variable "y"
>            
>             dumpprefs("y",y)        --    dump the Rexx variable "y"
> to the log
>
> ------------------------------------------------------------------------------------------------------------------------            
>
>     method setprefs(n=Rexx,v=Rexx)                     --    this
> method allocates a preference file and stores a Rexx variable in it
>
>              Log.i("1tta", "setprefs" n"="v)
>             prefs = getSharedPreferences(n.toString, 0)        --    
> open a preference file
>             editor = prefs.edit()                    --    init a
> preference change buffer
>             setpref(n,v,editor)                    --    call
> recursive store of Rexx values and indexes
>
>               editor.commit                        --    commit the
> preference changes to the file
>
>     method setpref(n=Rexx,v=Rexx,ed=Editor)                --    this
> method stores all indexed values by calling itself recursively
>              Log.i("1tta", "setpref" n"="v)
>            
>             tv=v                        --    save non-indexed value
>             loop i over v                    --    check each index
>                 tv = tv'['i']'                --    save the index
>                 setpref(n'['i']',v[i],ed)        --    save the value
>                 end
>                
>             ed.putString(n.toString,tv.toString)                --    
> store the base value and it's delimited indexes
>
> -------------------------------------------------------------------------------------------------------------------------        
>
>     method getprefs(n=Rexx) returns Rexx                --    this
> method locates a preference file and reads the Rexx variable stored in it
>
>              Log.i("1tta", "getprefs name="n)
>             prefs = getSharedPreferences(n.toString, 0)        --    
> open a preference file
>             return getprefs(n,prefs)                --    Build a Rexx
> indexed variable from the file
>
>     method getprefs(n=Rexx,p=SharedPreferences) returns Rexx    --    
> this method reads all indexed values by calling itself recursively
>              tv=p.getString(n,null)                    --    load base
> Rexx value and indexes
>              parse tv initval '[' indexes                    --    
> check for index delimiter
>              v=initval                            --    save base value
>              Log.i("1tta", "getprefs" n'='v)
>                           if indexes\="" then do                    
> --    we have indexed sub values
>                     indexes='['indexes                    --     for
> parse loop
>                                         loop while
> indexes\=""                --     loop for each index
>                            parse indexes '[' nextindex ']' indexes    
> --    get index name
>                            
> v[nextindex]=getprefs(n'['nextindex']',p)    --      restore indexed
> value
>                            end
>                                                end
>                                  return v                            
> --    return indexed Rexx variable
>                    
> ------------------------------------------------------------------------------------------------------------------------
>
>     method dumpprefs(n=Rexx,v=Rexx)                    --    this
> method dumps an indexed Rexx variable to the log by calling itself
>        
>              Log.i("1tta", "dumpprefs" n"="v)
>            
>             loop i over v
>                 dumpprefs(n'['i']',v[i])
>                 end
> -------------------------------------------------------------------------------------------------------------------------
>
>          
> _______________________________________________
> Ibm-netrexx mailing list
> [hidden email]
>
>

_______________________________________________
Ibm-netrexx mailing list
[hidden email]

Tom. (ths@db-123.com)