[REBOL] Re: a little help for a newbie?
From: antonr:iinet:au at: 18-Feb-2003 12:56
Dave,
You have (understandably) misunderstood
how FOREACH is used. The first argument
to foreach is a word or a block of words
that *you* name, that will be set to
successive values in the second argument.
ie. It just so happens that Gabriele chose
good words [name style] to refer to each
pair of elements in system/view/vid/vid-styles.
He could have used [cow dog] as long as the
action block was [print cow].
Check out this console session:
>> foreach [a b c] [1 2 3 4 5 6 7] [print [a b c]]
1 2 3
4 5 6
7 none none
You can see that, because I specified [a b c] (three words),
that three numbers at a time are taken from the block.
Now, if you examine system/view/vid/vid-styles:
>> probe system/view/vid/vid-styles
It gives you a really long block, that looks like this:
[face
make object! [
type: 'face
offset: 0x0
size: none
....
] IMAGE
make object! [
type: 'face
offset: 0x0
size: none
span: none
pane: none
] ....
....
....
]
(By the way, PROBE is really useful.)
What is it, though?
>> type? system/view/vid/vid-styles
== block!
What's inside it?
>> type? system/view/vid/vid-styles/1
== word!
>> type? system/view/vid/vid-styles/2
== object!
So the first thing inside is a word, and the second
thing is an object. If you continue, you see that the
third thing is again a word, and the fourth thing is
an object. So we have repeating pairs of elements.
We could have rewritten Gabriele's line:
foreach [word object] system/view/vid/vid-styles [print word]
Here, 'object is being used only so that we skip
over the objects. Nothing is being done with it
in the action block [print word].
This reminds me of the relatively new function
EXTRACT:
>> extract system/view/vid/vid-styles 2
== [face IMAGE BACKDROP BACKTILE BOX SENSOR KEY VTEXT ...
How to see the facets of a face?
A face is really just an object with some standard
face elements
in it. Do this to have a look at
the standard face:
>> probe face
Now to get the list of facets:
>> print mold first face
So our example:
>> print mold first system/view/vid/vid-styles/button
Anton.