[REBOL] EOF? Help please Re:(3)

2000-02-15 Thread icimjs

Hi Tiana,

if you run the following version of your code you will easily see what is
happening:

REBOL []

blk: read/lines %test.dat   

print "blk contains:"
foreach string blk [print mold string]
print "end blk contains"

while [not empty? blk]
[
print ["=" newline
mold to-block first blk newline
"="
]

d1:  second (to-block first blk) 
d1: parse d1 "-"
d1: to-date d1/1
print d1
blk: next blk 
print blk   
either not empty? blk [print "not empty"][print "empty"]
]

In short what happens is that when you read/lines your file, empty lines in
the file are contained in blk as emtpy strings:

[""]

When you check 
empty? blk

and blk is [""] then blk is not empty of course (it contains an element
which is an empty string). But the string is empty and therefore there is
no second element in the string. So 

d1:  second (to-block first blk) 

fails. to-block first blk will return an empty block [] and there is no
second element in the empty block.

It's safer to use pick (to-block first blk) 2 instead of second. The
advantage is that pick series index returns none when there is nothing to
pick instead of generating an error. 

  So you can say:

if d1: pick (to-block first blk) 2 [
  d1: parse d1 "-"
  d1: to-date d1/1
  print d1
]
blk: next blk 
print blk   
either not empty? blk [print "not empty"][print "empty"]

and only attempt to process d1 if it is not none.

Hope this helps,


;- Elan  [: - )]



[REBOL] EOF? Help please Re:(3)

2000-02-15 Thread ingo

Hi Tiana,

when your file has an empty last line, pointing at this line
your block is not yet empty, because it is pointing at "".
now try

to-block ""
== []

which has no second element. Thus the error.

One possible solution is:

blk: read/lines %test.dat
while [
   all [
   not empty? blk
   not none? d1: pick (to-block first blk) 2
   ]
] [
d1: parse d1 "-"
d1: to-date d1/1
print d1
blk: next blk
print blk
either not empty? blk [print "not empty"][print "empty"]
]

'pick does not raise an error, if the refereneced element does
not exist, but returns 'none.
'all returns 'true, if all of it's elemets return 'true


hope this helps,

regards,

Ingo


Those were the words of [EMAIL PROTECTED]:
 Hi, Andrew,
 Thanks for your reply. I don't think it is the problem of a simple while loop. I
 attached my script and input file in this msg:
 
 blk: read/lines %test.dat   
 while [not empty? blk]
 [
 d1:  second (to-block first blk) 
 d1: parse d1 "-"
 d1: to-date d1/1
 print d1
 blk: next blk 
 print blk   
 either not empty? blk [print "not empty"][print "empty"]
 ]
 
 test.dat:
 
 21-Feb-2000{ 2000/02/01-00:20  -  2000/02/08-21:48 }7629
 20-Feb-2000{ 2000/02/02-00:20  -  2000/02/08-21:48 }7629
 19-Feb-2000{ 2000/02/03-00:20  -  2000/02/08-21:48 }7629
 
 
 This test.dat file is generated by machine. The weird thing is if I open the
 file and clear its tail, the script worked fine. But if I added an empty newline
 at the end of file, it won't work. 
 ** Script Error: Out of range or past end.
 ** Where: d1: second (to-block first blk) 
 
 When rebol execute 
 
 either not empty? blk [print "not empty"][print "empty"]
 
 it always prints 'not empty' even the content of blk printed is blank.
 
 I'm so frustrated about this. Can u help me out?
 
 Thanks a million.
 
 Tiana
 

--  _ ._
ingo@)|_ /|  _| _  We ARE all ONE   www._|_o _   _ ._ _  
www./_|_) |o(_|(/_  We ARE all FREE ingo@| |(_|o(_)| (_| 
http://www.2b1.de/Rebol/ ._|  ._|