Home   Archive   Permalink



Setting the value of a variable whose name is from another ...

Second Layer Variables
I figured out how to retrieve the value of a 2nd layer variable. Example:
ProgVars: ["JobID" "JobDesc" "JobRef" etc.]
ProgVars/1 is equal to JobID
ProgVars/2 is equal to JobDesc
...etc.
Let's say that JobDesc is set to "Build Cabinet".
To retrieve the value of JobDesc I 'do ProgVars/2'
    
But I can't quite seem to SET the value of ProgVars/2, (ie: set JobDesc "some job desc") without hardcoding "JobDesc" into the program.
I've tried a few things including:
set reduce ProgVars/2 "some job desc" ; but no joy.
    
How do you set the value of a 2nd layer variable?
    


posted by:   Gordon Raboud       30-Nov-2014/22:16:44-8:00



I did this on try.rebol.nl :
ProgVars: ["JobID" "JobDesc" "JobRef" etc.]
;ProgVars/1 is equal to JobID
;ProgVars/2 is equal to JobDesc
print ProgVars/2
ProgVars/2: "My new description"
print ProgVars/2
probe progvars
    
It's output:
JobDesc
My new description
["JobID" "My new description" "JobRef" etc.]
== ["JobID" "My new description" "JobRef" etc.]
    
Is that what you meant?

posted by:   Arnold       1-Dec-2014/2:39:41-8:00



Hello Gordon,
    
I want to mention the resource of StackOverflow for programming Q&A:
    
http://stackoverflow.com/questions/tagged/rebol
    
(This forum is more suited to wandering discussions that StackOverflow forbids.)
    
It seems what you want is an object, but you aren't using one. You are using a symbolic block of strings, which is a poor fit if your intention is keys and values.
    
ProgVars: object [JobID: JobDesc: JobRef:]
ProgVars/JobDesc: "My new description"
probe ProgVars
    
That should give you:
    
make object! [
     JobID: none
     JobDesc: "My new description"
     JobRef: none
]

posted by:   Fork       1-Dec-2014/4:56:32-8:00



Thanks for some suggestion. I will go to stackoverflow with any future question.
    
    I want to keep the variable 'JobDesc', not change it. I want to change the value of 'JobDesc' to be "My new Description" not the value of ProgVars/2. I want to access the names of the variables at ProgVars/x, (1, 2, etc), to use as headers for a table and then the values of the variable whose name is at ProgVars/x to be used in the table body. The header won't change but of course the body of the table changes per row.
Plus the headers change depending on which table I access, so ProgVars/2 won't always be "JobDesc", and that is why if I can set the value of the variable whose name is at ProgVars/2 this one routine will work for all my tables.
    
Fork: I will try the suggestions in your post. thanks.

posted by:   GordR       1-Dec-2014/7:09:43-8:00



>> ProgVars: [JobID JobDesc JobRef]
== [JobID JobDesc JobRef]
>> JobDesc: "Build Cabinet"
== "Build Cabinet"
>> ProgVars/2
== JobDesc
>> get ProgVars/2
== "Build Cabinet"

posted by:   Nick       1-Dec-2014/10:32:08-8:00



Be sure to use word! values (instead of string! values) in your block, and use 'get to obtain the value of the word.

posted by:   Nick       1-Dec-2014/12:05:26-8:00



Thanks Nick. "get ProgVars/2" makes more sense than "do ProgVars/2".
And my error messages was always something about a "string" and yes I tried to convert to-word! but that was just doing it wrong!    
More to experiment with.

posted by:   GordR       1-Dec-2014/14:25:50-8:00