[REBOL] Confusion about foreach
From: swhite:ci:bloomington:mn:us at: 17-Mar-2006 9:37
There must be some simple little thing I don't understand about foreach, or about conditions,
and I am wondering if someone can explain. I have isolated the problem into a test script
at the end of this message. The thing I am trying to program is: I have a text file containing
several types of fixed-format records. It is for transferring money to the bank. A type
of record is identified by a single digit in the first character position of the record.
In the file, there is one particular record I want to find, the only one with a particular
record type code in the first position (a trailer record, but not the last record in
the file, so I can't just use last. ) So, I read the file into memory with read/lines.
Each line becomes a string. I loop through the block of strings (lines) and check the
first character of each. When I find the record type (first character) I am looking for,
I save that record in another place so I can examine it more detail later in the program.
In the following test script, if you run it, you see that "print TEST-RECORD/1" is executed
and displays the first character of every record. BUT, HOLD-TEST-RECORD never gets set
to anything (besides its initial value of a null string) and "print 'We found record
type 4'" never is executed. So that condition of 'if TEST-RECORD/1 = "4"' is never true.
What am I doing wrong? It looks so obviously correct. I read the manual, and I am "sure"
I have seen code like that before. Thank you. ;;;; test script REBOL [ ] TEST-FILE: [
"1this is record one" "2this is record two" "3this is one of many type 3 records" "3this
is one of many type 3 records" "4this is the record we want" "9this is some ending record"
] HOLD-FOUND-RECORD: copy "" foreach TEST-RECORD TEST-FILE [ print TEST-RECORD/1 if TEST-RECORD/1
= "4" [ HOLD-FOUND-RECORD: copy TEST-RECORD print "We found record type 4" ] ] print
rejoin ["HOLD-FOUND-RECORD=" HOLD-FOUND-RECORD] ;;;; end of test script Steven White
City of Bloomington 1800 W Old Shakopee Rd Bloomington MN 55431-3096 USA 952-563-4882
(voice) 952-563-4672 (fax) steven.white-ci.bloomington.mn.us