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

Seeming 'oddity' using the 'offset' parm with 'copy/part' ..........

 [1/6] from: John::Dutcher::highmark::com at: 7-Mar-2006 8:46


I'm trying to discover the possible cause for an oddity when using the 'offset' parameter as in the 'substr' funtion in the small script below. This sample exercise is a miniaturization of the same phenomenon which I noticed when creating a much larger 'series' item by reading fixed-length records from a file one at a time and valuing some variables with pieces of those records. The notes at the right of the 2nd (uncommented) 'for' loop below indicate that I get the same results and values in the 'print' results when using 'different' starting positions in the series call to 'substr' which uses 'starting position' and 'length' as its parameters. The commented 'for' loop which appears first, delivers the correct results.....but I really need to understnd why the 2nd loop can use different position 'starting values' in the call ....... and output the same results.....it doesn't seem 'right and proper'. (There are (3) instances of a simulated record in the variable 'sample' below, each is (38) characters long). Thanks............ John D. ******************************************************************** Rebol[Title: "Sample copy/part script"] substr: func [record offset len] [ copy/part at record offset len ] sample: "0015342HORNBECK EDNA A0024667BADGER DONALD R0035457ECONOMOU THOMAS S" ;for i 1 114 38 [ ; ; recnbr: substr sample i + 0 3 ; regnbr: substr sample i + 3 4 ; lname: substr sample i + 7 15 ;This version using the 'i' varibale plus what I consider the 'correct' adjustment ; fname: substr sample i + 22 15 ; produces the expected result presuming 'series' start at offset '1' ; minit: substr sample i + 37 1 ; print [recnbr regnbr lname fname minit] ; ;] ; should be able to use 'fixed' offset values and get correct result also ; doesn't seem as though more than one 'starting position' could possibly ; deliver the same results ........and yet.....it seems to. for i 1 114 38 [ rec1: substr sample i 38 recnbr: substr rec1 0 3 ; zero or 1 offset gives same result regnbr: substr rec1 4 4 ; 3 or 4 offset does NOT give same result lname: substr rec1 8 15 ; 7 or 8 offset does NOT give same result fname: substr rec1 22 15 ; 22 or 23 offset gives same result minit: substr rec1 38 1 ; 37 or 38 offset does NOT give same result print [recnbr regnbr lname fname minit] ] halt

 [2/6] from: John:Dutcher:highmark at: 7-Mar-2006 9:07

Re-post of 'offset' issue...sorry...the first one came out so ugly...........


I'm trying to discover the possible cause for an oddity when using the 'offset' parameter as in the 'substr' funtion in the small script below. This sample exercise is a miniaturization of the same phenomenon which I noticed when creating a much larger 'series' item by reading fixed-length records from a file one at a time and valuing some variables with pieces of those records. The notes at the right of the 2nd (uncommented) 'for' loop below indicate that I get the same results and values in the 'print' results when using 'different' starting positions in the series call to 'substr' which uses 'starting position' and 'length' as its parameters. The commented 'for' loop which appears first delivers the correct results.....but I really need to understand why the 2nd loop can use different position 'starting values' in the call ....... and output the same results.....it doesn't seem 'right and proper'. (There are (3) instances of a simulated record in the variable 'sample', each is (38) characters long). Thanks............ John D. ******************************************************************** Rebol[Title: "Sort dsysmst.txt to name sequence"] substr: func [record offset len] [ copy/part at record offset len ] sample: "0015342HORNBECK EDNA A0024667BADGER DONALD R0035457ECONOMOU THOMAS S" ;In this for loop ....all results are as expected ;for i 1 114 38 [ ; ; recnbr: substr sample i + 0 3 ; regnbr: substr sample i + 3 4 ; lname: substr sample i + 7 15 ; fname: substr sample i + 22 15 ; minit: substr sample i + 37 1 ; print [recnbr regnbr lname fname minit] ; ;] ;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] ] halt

 [3/6] from: greggirwin::mindspring::com at: 7-Mar-2006 8:21


Hi John, Your sample record seems a bit mis-formatted but, other than that, it works fine here. The results I see are: 001 5342 HORNBECK EDNA A 002 4667 BADGER DONALD R0035457ECONOM U THOMAS S So, record 2 needs padding on the names, but it works. -- Gregg

 [4/6] from: anton::wilddsl::net::au at: 9-Mar-2006 15:32


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"
== "abcd"
>> at s 0
== "abcd"
>> at s 1
== "abcd"
>> at s 2
== "bcd" And compare with SKIP:
>> skip s 0
== "abcd"
>> skip s 1
== "bcd" 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 minit] 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
<<quoted lines omitted: 16>>
> 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 [ any [ 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 together. Anton.

 [5/6] from: John:Dutcher:highmark at: 9-Mar-2006 5:14


Anton .....your response is very insightful ..... and right on the money. Thanks so much !!! John D. Anton Rolls <anton-wilddsl.net.au> Sent by: rebol-bounce-rebol.com 03/08/2006 11:32 PM Please respond to rebolist-rebol.com To rebolist-rebol.com cc Subject [REBOL] Re: Re-post of 'offset' issue...sorry...the first one came out so ugly........... 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"
== "abcd"
>> at s 0
== "abcd"
>> at s 1
== "abcd"
>> at s 2
== "bcd" And compare with SKIP:
>> skip s 0
== "abcd"
>> skip s 1
== "bcd" 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 minit] 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
<<quoted lines omitted: 16>>
> 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 [ any [ 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 together. Anton.

 [6/6] from: anton:wilddsl:au at: 10-Mar-2006 1:20


You're welcome. :) Anton.

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