Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Apparent newbie question

 [1/11] from: Steven::White::ci::bloomington::mn::us at: 20-Nov-2003 11:28


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
>>
*_*_*_*_* End of test results *_*_*_*_ Steven White City of Bloomington 1800 W Old Shakopee Rd Bloomington MN 55431-3096 USA 952-563-4882 (voice) 952-563-4672 (fax) [steven--white--ci--bloomington--mn--us]

 [2/11] from: tomc:darkwing:uoregon at: 20-Nov-2003 9:52


On Thu, 20 Nov 2003, 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
<<quoted lines omitted: 5>>
> print ["Length of TEST-BLOCK is " length? TEST-BLOCK] > if length? TEST-BLOCK < 4 [
if (length? TEST-BLOCK) < 4 [ or if 4 >= length? TEST-BLOCK [ should work has to do with how tightly bound to its arguments an op is. someone more verbose could say more

 [3/11] from: maximo:meteorstudios at: 20-Nov-2003 12:55


ahhh something I got a long time to "GET" someone with more academic qualities (and more time) will be able to explain better, but quickly, you must put any computed values AFTER the operator, or 'IF (and 'EITHER) will use the words after the operator as the block to execute. example: if length? TEST-BLOCK < 4 [ print "block too short" ] becomes (in non rebol syntax): if (lenght? test-block) == true then do 4 in other words, the operator is not part of the expression being evaluated. you have to do this instead: if 4 >= length? TEST-BLOCK [ print "block too short" ] OR use parenthesis to properly encapsulate the complete expression: if (length? TEST-BLOCK < 4) [ print "block too short" ] the first method is prefered, because I have read that grouping expressions in parenthesis slows down evaluation (but I have never tested this myself). but if a microscopic speed difference is not a problem, then the second fix is usually more readable. HTH -MAx --- You can either be part of the problem or part of the solution, but in the end, being part of the problem is much more fun.

 [4/11] from: lmecir:mbox:vol:cz at: 20-Nov-2003 19:03


Hi Steven,
> 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
<<quoted lines omitted: 7>>
> print "block too short" > ]
length? test-block < 4 is equivalent to: length? (test-block < 4) , while you wanted: (length? test-block) < 4 -L

 [5/11] from: didec:tiscali at: 20-Nov-2003 19:08


Re: Apparent newbie question Hi Steven
> 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?
Not dumb, but frequent error
> Thank you. > *_*_*_*_* Sample script *_*_*_*_*
<<quoted lines omitted: 4>>
> print "block too short" > ]
for the interpreter your code is execute like if it was like this : if length? (TEST-BLOCK < 4) [ ... ] So you can write : if (length? TEST-BLOCK) < 4 [ ... ] or if 4 > length? TEST-BLOCK [ ... ] DideC

 [6/11] 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
<<quoted lines omitted: 13>>
> ** 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

 [7/11] from: ingo:2b1 at: 20-Nov-2003 19:12


Hi Tom, sorry, your version is buggy, you only have to *turn around* the test-case, not refactor it ... Tom Conlin wrote: <...>
> if (length? TEST-BLOCK) < 4 [ > > or > > if 4 >= length? TEST-BLOCK [
<...> Just try starting at 3 going on to 5 and check if both versions give you the same results. Kind regards, Ingo

 [8/11] from: gchiu:compkarori at: 24-Dec-2003 22:48


Re: Apparent newbie question
> if length? TEST-BLOCK < 4 [ > print "block too short" > ] >
Try .. (length? test-block) < 4 -- Graham

 [9/11] from: maximo:meteorstudios at: 20-Nov-2003 14:17


your so right... why did I think like that tooo... ?!!?! I must be in need of time off ;-) -MAx

 [10/11] from: brett:codeconscious at: 21-Nov-2003 10:01


Hi Steven, As you've seen from all the answers so far, precedence matters with operators. The Core guide says (in the Math section) that there are just two rules: Expressions are evaluated from left to right. Operators take precedence over functions. For a long time while I was learning REBOL I basically ignored operators like +, -, * etc. Instead I used their function equivalents: Add, Subtract, Multiply. Once I got comfortable with the precedence rules of operators I started using a mix. Eg. Add 3 * X 4 * Y instead of (3 * X) + (4 * Y) Every now and then I get the precedence stuff mixed up and instead of inserting heaps of paren! I revert to using the function style. Most recently, after Joel's post on coding habits and the effect of changing them, I've started using a different habit for If conditions more akin to the other posts. if x = 4 * Y [...] Regards, Brett.

 [11/11] from: antonr:iinet:au at: 21-Nov-2003 18:09


Rebol is strict about left to right evaluation. I remember Holger commenting about in-fix operators (operators with two surrounding arguments) that when you see an expression like this: 1 > 2 then you need to imagine that the ">" moves to the front:
> 1 2
(to be a prefix operator.) Now it looks like any other function. So in the expression: length? test-block < 4 you should imagine this: length? < test-block 4 Now rebol will evaluate like this: length? (< test-block 4) which leads to the error: ** Script Error: Expected one of: block! - not: integer! Who expected it? The < operator, of course. Anton.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted