Stupid question from a newbie
[1/4] from: FC2COLLINS::peoplepc::com at: 19-Feb-2006 21:24
I have this little bit of code :
mailbox: read pop://XXX:XXXXX-mail.XXX.net
ctr: 0
foreach message mailbox [
print pick mailbox ctr
wait 0:00:5
remove at mailbox ctr
ctr: ctr + 1
]
It seems to work perfectly happily, however it always skips the first
message. How do I make it read the first message in the list?
[2/4] from: greggirwin::mindspring::com at: 20-Feb-2006 10:02
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.
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
[3/4] 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 :
<<quoted lines omitted: 14>>
> 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: []
<<quoted lines omitted: 23>>
> 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
[4/4] from: nick::guitarz::org at: 21-Feb-2006 10:48
Hi Timothy,
Maybe these examples are helpful:
; read the entire mailbox contents with "read":
mailbox: read pop://user:pass-website.com
; three ways to loop through the block of retrieved messages:
foreach message mailbox [editor message]
forall mailbox [editor first mailbox]
c: 1
while [c <= length? mailbox] [
editor pick mailbox c
c: c + 1
]
; or, open the mailbox as a port, read each mail item individually (directly
from the server), and then close the port:
mailboxx: open pop://user:pass-website.com
forall mailboxx [
editor first mailboxx
]
close mailboxx
Quoting Timothy Collins <FC2COLLINS-peoplepc.com>:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted