Need help printing first elm of block
[1/4] from: charliew::drte::com at: 21-May-2001 10:43
Please help me understand what is happening here.
>> aVar: "one"
== "one"
>> bVar: "two"
== "two"
>>
>> blk: [aVar bVar]
== [aVar bVar]
[2/4] from: joel:neely:fedex at: 21-May-2001 10:20
Hi, Charles,
Charles Wardell wrote:
> Please help me understand what is happening here.
> >> aVar: "one"
<<quoted lines omitted: 11>>
> aVar <-------------------- Why?
> >>
Because PRINT does an implicit REDUCE on the argument block.
BLK contains two WORD! values, but if you force evaluation,
each of those words is bound to a string.
>> foreach item blk [print [type? :item tab item]]
word aVar
word bVar
>>
If PRINT didn't do the implicit REDUCE, then you wouldn't
be able to put expressions inside a block argument to PRINT
and get the values of those expressions as output without
explicitly forcing the reduction yourself. Since it's most
likely that we really want such expressions evaluated, the
current policy is a net win.
>> caption: "The answer is:"
== "The answer is:"
followed by
>> print [caption 2 + 2]
The answer is: 4
>> print-without-reducing [caption 2 + 2]
[caption 2 + 2]
I suspect that most of the time we'd expect the first output
rather than the second. (I cheated... Here's the missing
definition:
>> print-without-reducing: func [b [block!]] [print mold b]
in case you were interested.)
So, the next question is "Which do you want?" REBOL lets you
decide whether you want the evaluation, but that means you have
to specify which you want. If you do want reduction for the
items in the block, you can use any of the following:
>> print get first blk
one
>> print first reduce blk
one
On the other hand, if you want to suppress evaluation of the
entire block, you can use any of these:
>> print [blk]
aVar bVar
>> print mold blk
[aVar bVar]
Ya payz ya money, ya getz ya choyse!
-jn-
[3/4] from: arolls:bigpond:au at: 22-May-2001 1:47
Hi,
blk is just a block of words.
You specifically want to evaluate the
first word in the global context.
What is its value in the global context?
(It could have a different value in a
different context).
Anyway, to get the value of the first
word in blk, try this code:
print get first blk
or:
print first reduce blk
or:
blk: reduce [aVar bVar]
== ["one" "two"]
print first blk
== "one"
Anton.
[4/4] from: joel:neely:fedex at: 21-May-2001 11:07
Ermmmm... Correcting small glitch in wording, possibly misleading.
Joel Neely wrote:
> Because PRINT does an implicit REDUCE on the argument block.
> BLK contains two WORD! values, but if you force evaluation,
<<quoted lines omitted: 3>>
> word bVar
> >>
Of course each of those words is set to a string, whether or not
you force evaluation! What I meant to say was:
...if you force evaluation, you get the string value to
which each of those words is set.
Sorry for any potential confusion!
-jn-
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted