[REBOL] Re: Stupid question from a newbie
From: volker:nitsch:gmai:l at: 21-Feb-2006 16:18
Hi Timothy, Gregg,
On 2/20/06, Gregg Irwin <greggirwin-mindspring.com> wrote:
> Hi Timothy,
>
> TC> I have this little bit of code :
> TC> mailbox: read pop://XXX:XXXXX-mail.XXX.net
> TC> ctr: 0
> TC> foreach message mailbox [
> TC> print pick mailbox ctr
> TC> wait 0:00:5
> TC> remove at mailbox ctr
> TC> ctr: ctr + 1
> TC> ]
>
> TC> It seems to work perfectly happily, however it always skips the first
> TC> message. How do I make it read the first message in the list?
>
> You're kind of mixing your metaphors there. The READ call will return
> a block of messages (see also: IMPORT-MESSAGE), and FOREACH will
> iterate through that block; so you don't need to use PICK to get a
> message from the block ('message already references it), but it's not
> changing the offset of 'mailbox. Also, keep in mind that REBOL series
> are one-based, not zero-based.
I think the main problems is this:
;from http://www.rebol.com/nutshell.html
mail: open pop://luke:r2d2-rebol.com
while [not tail? mail] [
either find first mail "spam" [
remove mail
][
mail: next mail
]
]
close mail
I guess Timothy saw something like this, and mixed something on his own.
The little, but important difference is, this example uses 'open,
Timothys 'read.
(As Gregg pointed out, but IMHO a bit hidden)
The difference is, 'open works directly on the mailbox.
'Read reads the whole mailbox and returns all mails in a block.
Ports (resulting from 'open) and blocks (resulting from copy) work a
little different.
With blocks you use pick. There is no need for a remove.
With ports, you do it sequentially, and skip or remove to advance.
Timothy picks and removes at the same time. This way he "double-steps".
The pick skips a message, this is ok.
The remove removes a message, thats ok too.
Both together skip a message and remove a message,
and thus every second message is lost.
> Try this to see what's happening:
>
> block: []
> repeat i 30 [append block i]
> ctr: 0
> foreach val block [
> print [ctr pick block ctr]
> remove at block ctr
> ctr: ctr + 1
> ]
> print mold block
>
> If you want to remove messages from a POP mailbox, you should use OPEN
> on the mailbox, so it's a port you can work against, then close it when
> you're done.
>
> The first thing I would suggest is to play around with some plain old
> loop and series ops in the console. Look at how FOREACH, FORALL,
> FORSKIP, etc. work. Practice iterating over series using WHILE and
> NEXT, using REPEAT with path notation to access values (e.g. repeat i
> length? block [print block/:i]).
>
> The concepts of series access and navigation by index (PICK, POKE,
> series/n), ordinal (FIRST, SECOND, etc.), and position (NEXT, BACK,
> SKIP, AT), are very important. Take some time to understand them and
> it will pay big rewards.
>
> HTH!
>
> -- Gregg
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler