[REBOL] Re: Re-post of 'offset' issue...sorry...the first one came out so ugly...........
From: John:Dutcher:highmark at: 9-Mar-2006 5:14
Anton .....your response is very insightful ..... and right on the money.
Thanks so much !!!
Sent by: rebol-bounce-rebol.com
03/08/2006 11:32 PM
Please respond to
[REBOL] Re: Re-post of 'offset' issue...sorry...the first one came out so
Rebol uses 1-based indexing (not 0-based, like other languages).
This is probably the source of confusion between the 0 or 1 offset.
Let's look at the behaviour of AT:
>> s: "abcd"
>> at s 0
>> at s 1
>> at s 2
And compare with SKIP:
>> skip s 0
>> skip s 1
So AT is 1-based, but you can use SKIP to get 0-based indexing.
I think the problem with fname (first name) is that it is preceded
by padding spaces, therefore you don't notice when you accidentally
collect one and print it. (It seems this confusion also combined with
the first problem.) To be more sure of what I am printing,
I usually MOLD the values, eg:
print [mold recnbr mold regnbr mold lname mold fname mold
or you could use REMOLD:
print remold [recnbr regnbr lname fname minit]
PROBE is also your debugging friend, because you can put it anywhere
and it doesn't affect the operation of the code.
> substr: func [record offset len] [
> copy/part at record offset len
> sample: "0015342HORNBECK EDNA A0024667BADGER DONALD
> R0035457ECONOMOU THOMAS S"
> ;In the for loop below ......
> ; zero or 1 offset gives same result
> ; 3 or 4 offset does NOT give same result
> ; 7 or 8 offset does NOT give same result
> ; 22 or 23 offset gives same result
> ; 37 or 38 offset does NOT give same result
> for i 1 114 38 [
> rec1: substr sample i 38
> recnbr: substr rec1 0 3
> regnbr: substr rec1 4 4
> lname: substr rec1 8 15
> fname: substr rec1 22 15
> minit: substr rec1 38 1
> print [recnbr regnbr lname fname minit]
And now for a more rebolish solution using PARSE:
sample: rejoin [
"0015342HORNBECK EDNA A"
"0024667BADGER DONALD R"
"0035457ECONOMOU THOMAS S"
digit: charset [#"0" - #"9"]
parse/all sample [
copy recnbr 3 digit
copy regnbr 4 digit
copy lname 15 skip
copy fname 15 skip
copy minit 1 skip
(print remold [recnbr
regnbr lname fname minit])
I've assumed the fields are fixed length for each record,
although your posting seemed to put "BADGER DONALD" close