Setting argument values in objects
[1/5] from: moeller_thorsten:gmx at: 5-Nov-2001 10:54
Hi list,
i have the following problem with objects.
I have an object called "proctype-obj" having about ten arguments for
different processortypes.
I have variable ( word ) containing a string which is the path to the
objects argument like
proc: "proctype-obj/IntelPentium(R)processor"
I have another variable containig an integer value like
count: 2
How can i set the argument in the object
(proctype-obj/IntelPentium(R)processor) to the value of the count variable???
Please help.
Thanks
Thorsten
[2/5] from: greggirwin:mindspring at: 5-Nov-2001 9:29
Hi Thorsten,
Hopefully I understand your need correctly. If so, this might work.
A short time ago, Ammon Johnson cooked up a nifty way to *get* a value from
an object given a path. I just made a quick mod to set the value and it
worked in the one test I ran. :) Here is his original and my modified
version (which you would use to set the value). Let me know if it works. I
left the print statements in so you can see what it generates to do its job.
; Ammon Johnson's function
get-path-obj: func [str[string!] /local return-val][
return-val: copy []
tmp-val: parse str "/"
insert/dup tail return-val [get in] ((length? tmp-val) - 1)
append return-val [get]
foreach val tmp-val [
append return-val compose [(to-lit-word :val)]
]
print form return-val
return do compose return-val
]
;-- SET version of GET-path-obj. (Gregg Irwin)
set-path-obj: func [str[string!] value /local return-val][
return-val: copy []
tmp-val: parse str "/"
insert/dup tail return-val [get in] ((length? tmp-val) - 1)
append return-val [get]
foreach val tmp-val [
append return-val compose [(to-lit-word :val)]
]
change return-val [set] ; change first 'get to 'set
append return-val value ; 'set takes the value
print form return-val
return do compose return-val
]
HTH!
--Gregg
[3/5] from: rotenca:telvia:it at: 5-Nov-2001 18:19
> Hi list,
Hi
> i have the following problem with objects.
> I have an object called "proctype-obj" having about ten arguments for
<<quoted lines omitted: 6>>
> How can i set the argument in the object
> (proctype-obj/IntelPentium(R)processor) to the value of the count
variable???
1) Pay attention, you can't call a variable with this name:
IntelPentium(R)processor
because () is a paren! datatype
2) If proctype-obj is a global word, you can try:
do reduce [to-set-path load proc 'count]
3) if both words are local to the word 'a for example, you must bind it before
doing it:
do bind reduce [to-set-path load proc 'count] 'a
4) if 'count it is not co-local to 'a, you must first bind the loaded proc and
then append to the block to Do the word 'count
---
Ciao
Romano
[4/5] from: geza67:freestart:hu at: 5-Nov-2001 23:13
Hello Thorsten,
> proc: "proctype-obj/IntelPentium(R)processor"
First, get rid of the parens! REBOL will parse them as a paren! value.
> (proctype-obj/IntelPentium(R)processor) to the value of the count variable???
If you have only one-level deep objects (i.e. not a/b/c),
set in proctype-obj 'IntelPentiumRprocessor
Below you find two functions, allowing to use the normal path
notation, even in case of nested objects. SETP works with object-path
lists only, while in the first list argument of SETX you can put even
(still) nonexistent words (and objects, naturally, even mixed) to set.
--------------------------
rebol []
setp: function [{Set multiple values at a time on block of objects (in path notation)}
where-list [any-block!] value-list [any-block!]][lhead ltail i][
i: 0
foreach elem where-list [
i: i + 1
if (type? :elem) = path![
ltail: last lhead: to-block :elem
lhead: head remove back tail lhead
do compose [set/any in (to-path lhead) (to-lit-word ltail)(pick value-list i)]
]
]
]
setx: function [{Set multiple values at a time on block of
objects (in standard path notation) or words}
where-list [any-block!] value-list [any-block!]][lhead ltail i][
i: 0
foreach elem where-list [
i: i + 1
do compose [set/any
(switch type?/word :elem [
path! [
ltail: last lhead: to-block :elem
lhead: head remove back tail lhead
compose [in (to-path lhead)]
]
word! [
ltail: :elem
[]
]
])
(to-lit-word ltail)(pick value-list i)]
]
]
a: context [ b: context [c: none]]
d: context [ e: none]
x: 'a/b/c
setx [a/b/c d/e y z] [1 2 3 4]
setp [a/b/c d/e] [1 2]
? a
? d
probe y
probe z
--------------------------
Hope, this helps!
--
Best regards,
Geza mailto:[geza67--freestart--hu]
[5/5] from: nitsch-lists:netcologne at: 6-Nov-2001 4:02
RE: [REBOL] Setting argument values in objects
[moeller_thorsten--GMX--De] wrote:
> Hi list,
> i have the following problem with objects.
<<quoted lines omitted: 7>>
> How can i set the argument in the object
> (proctype-obj/IntelPentium(R)processor) to the value of the count variable???
hmm, do you really need "IntelPentium(R)processor"?
because putting "()" in rebol-pathes will not work..
building and using paths is a bit complicated.
could you use a hash! instead, like
proctype: make hash!["IntelPentium(R)processor" 2 "mychip" 3]
something like (untested)
associate: func["change or append" block key value /local here][
either here: find/skip block key 2
[here/2: value] [repend block [key value]]
]
.. associate proctype "mychip" 24
to add entries and
select/skip block "mychip" 2
to get it back.
without [/skip .. 2]
instead of finding a key 'find could find also a value,
so i use /skip's for the general case.
if you are sure no value is like a key, you can drop them.
some ways are if you use string-keys, never store
values in strings, or make them somehow different.
- Volker
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted