[REBOL] Re: [Fwd: Issue with block command 'NEXT']
From: ingo::2b1::de at: 24-Dec-2003 22:22
there are a few pitfalls lurking in your code ...
> The code fragment below shows promise for deleting a block from a
> rebol style
> database file. It finds the block within the database properly and
> reports that
> it removed it....it saves the file, and the expected block is gone
> and the file
> has compressed itself to close the gap perfectly. BUT....the next
> of the 'For loop' fails to find a good series to look at, as
> evidenced by the
> error msg. also shown below (I let the loop continue in case there
> additional blocks meeting the search criteria) ???
It's not the next iteration of the for loop that
runs into an error, but the last. Remember? You
let it run to the length of the block, but
inbetween the loop you remove one item, so the
loop runs over the end of your block.
> As a side item, I was puzzled that my originally coded 'remove pick
> database i'
> originally on line (5) didn't work (only removed first value in the
> block)i, but
> 'remove next database i' seemed to remove entire block (as was
'pick "picks" an element out of a block, in other
words, what 'pick returns has no indication that
it is part of a block.
'remove on the other hand, removes the _current_
_element_ from a block.
Can you put it together yourself?
'pick returns the element, 'remove sees "ah,
that's a block", so it removes the first value
from the block, the _element_ returned.
What you are really looking for is 'at which
returns a block at a specified position.
So here's my version:
remove-data: func [lottery-name ticket-value /local i] [
; loop is faster, and we want to take care of the incrementation
loop length? database [
; use either to only increment i if no removal took place
either all [
equal? first pick database i lottery-name
equal? second pick database i ticket-value
][ ; end all
remove at database i ; use at
print [" "]
print ["Yes, the ticket: " lottery-name " " ticket-value " was
;save db-file database
; no need to increment i here, we're on the next element automatically
; increment i only if no element has been deleted
i: i + 1
] ;end either
; make sure we don't run over the end of the database, because of
if i > length? database [break]
] ;end loop
] ;end func
db: copy database: [[1 2][2 3][4 5][4 5][4 3 ][4 7][4 5][6 7][8 9]]
remove-data 4 5
I hope that helps,