[REBOL] Re: Apparent newbie question
From: ingo:2b1 at: 20-Nov-2003 19:05
Hi Steven,
been there, done that, but never got the T-Shirt ;-)
Steven White wrote:
> This must be something dumb, but I want to test the length of a block
> and can't seem to do it, yet I can print the length of the block. What
> am I missing?
>
> Thank you.
>
> *_*_*_*_* Sample script *_*_*_*_*
>
> REBOL [ ]
>
> TEST-BLOCK: [ XXXXXXXX YYYYYYYYY 100 200 ]
>
> print ["Length of TEST-BLOCK is " length? TEST-BLOCK]
>
> if length? TEST-BLOCK < 4 [
> print "block too short"
> ]
>
> *_*_*_*_*_ Result of running test script *_*_*_*_*
>
>>>do %lentest.r
>
> Length of TEST-BLOCK is 4
> ** Script Error: Expected one of: block! - not: integer!
> ** Where: halt-view
> ** Near: if length? TEST-BLOCK < 4
What's happening here is Rebol evaluation order hitting hard, to
understand, let's step through it:
length? is a function which expects a block to test its length, so it
tries to grab your TEST-BLOCK.
The TEST-BLOCK in turn is followed by '< which expects to values, one on
each side, and returns if the left one is smaller than the right-hand one.
So far, so good, but until now it has a TEST-BLOCK of type block! and an
integer, and, of course, this doesn't work.
You have two possibilities to go on:
- turn your line around
4 > length? test-block
- use parens
(length? TEST-BLOCK) < 4
I hope that helps,
Ingo