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

Confusion about foreach

 [1/7] 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

 [2/7] from: carlos::lorenz::gmail::com at: 17-Mar-2006 12:46


Steven Maybe you should use copy/part to get the leftmost part of the string and then test it See: 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" ]
>> print copy/part TEST-FILE/5 1
4 2006/3/17, Steven White <swhite-ci.bloomington.mn.us>:
> 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
<<quoted lines omitted: 53>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- *:-.,_,.-:*'``'*:-.,_,.-: Carlos Lorenz *:-.,_,.-:*'``'*:-.,_,.-:

 [3/7] from: robert_brandner:gmx:at at: 17-Mar-2006 16:49


Hi Steve, I think you have to write #"4" instead of "4" as a string consists of single characters. regards, Robert Steven White wrote:

 [4/7] from: rebolek:gma:il at: 17-Mar-2006 17:00


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
<<quoted lines omitted: 74>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- / Boleslav Brezovsky http://krutek.info \

 [5/7] 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. > >
<<quoted lines omitted: 42>>
> To unsubscribe from the list, just send an email to > lists at rebol.com with unsubscribe as the subject.
-- / Boleslav Brezovsky http://krutek.info \

 [6/7] from: greggirwin::mindspring::com at: 17-Mar-2006 10:12


Hi Steve, SW> Robert is right, string consists of single characters. Look at this SW> console session:
>>> record: "abcd"
SW> == "abcd"
>>> record/1
SW> == #"a"
>>> type? record/1
SW> == char! Don't forget to dump stuff out to the console, or a file (look up ECHO) just to see what data is there when this kind of thing happens. There are always times where you think the data is right, but apps don't work. Another thing to remember when doing so, is to use MOLD on values, so you can see the actual REBOL lexical form of the data. -- Gregg

 [7/7] from: rebolforces::gmail at: 14-Apr-2006 20:51


Also it is often better to use PROBE rather than PRINT for debugging. If you where using it instead of print above, the char! vs string! would have been more obvious. e.g
>> print [1 + 2]
3
>> probe [1 + 2]
[1 + 2] == [1 + 2]
>> a: "12345"
== "12345"
>> a/2
== #"2"
>> print a/2
2
>> probe a/2
#"2" == #"2" -- Allen K

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