[REBOL] Re: Confusion about foreach
From: swhite:ci:bloomington:mn:us at: 17-Mar-2006 10:30
Yes, thank you all, that was it. My brain still works in COBOL.
There is an explanation in "REBOL for Dummies" on page 83 (which I
found now that I know what to look for).
>>> rebolek-gmail.com 3/17/2006 10:00 AM >>>
Hi Steve,
Robert is right, string consists of single characters. Look at this
console
session:
>> record: "abcd"
== "abcd"
>> record/1
== #"a"
>> type? record/1
== char!
So, in your code change this line:
if TEST-RECORD/1 = "4" [
to
if TEST-RECORD/1 = #"4" [
and it should work.
By, Rebolek
On 3/17/06, Robert Brandner <robert_brandner-gmx.at> wrote:
>
> Hi Steve,
>
> I think you have to write #"4" instead of "4" as a string consists
of
> single
> characters.
>
> regards,
> Robert
>
> Steven White wrote:
> > 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
>
> --
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
>
--
/
Boleslav Brezovsky
http://krutek.info
\