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

[Fwd: Issue with block command 'NEXT']

 [1/1] from: ingo::2b1::de at: 24-Dec-2003 22:22


Hi, there are a few pitfalls lurking in your code ...
> Hi, > The code fragment below shows promise for deleting a block from a
<<quoted lines omitted: 10>>
> are > 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 > intended).
'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: [REBOL [] remove-data: func [lottery-name ticket-value /local i] [ i: 1 ; loop is faster, and we want to take care of the incrementation ourselves 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 removed"] ;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 deleted elements 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 database ] I hope that helps, Ingo

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