[Date Prev][Date Next][Thread Prev][Thread Next]
[Author Index]
[Date Index]
[Thread Index]
[SQR-USERS Info]
[SQRUG Home Page]
Re: [sqr-users] Flat-File Manipulation Help
Harold,
A few general points to remember:
1) Recursion in SQR is arcane in the extreme. Don't try calling a
procedure from within itself until you are VERY sure you know what you are
doing.
2) SQR can handle this kind of flat file string manipulation just fine, but
it is not the main thrust of the language. Most of the commands won't
apply. If you have to do a lot of things like this you may want to use Perl.
3) These kinds of file often come from mainframe computers which often
contain the null character (character #0). SQR uses null terminated
strings internally, so when you read a line everything after the first null
disappears. If this is the case, you can either fix the file with another
tool or read each line one byte at a time.
If your file does not have nulls in it, the following program should work.
HTH,
Eric
!I like to give the file handles names so I don't forget them.
#define INFILE 1
#define OUTFILE 2
begin-program
do move_file
do write_new_file
end-program
!----------------------------------------------------------------
begin-procedure move_file
if rename('FLATtest.txt', 'FLATtest.orig') != 0
show 'Error renaming file'
stop
end-if
end-procedure move_file
!----------------------------------------------------------------
begin-procedure write_new_file
! you got this next line right
open 'FLATtest.orig' as {INFILE} for-reading record=78:vary
open 'FLATtest.new' as {OUTFILE} for-writing record=78:vary
while 1
read {INFILE} into $line:78
if #end-file
break
end-if
if substr($line, 1, 1) = ' '
let $seg_1 = substr($line, 2, 24)
let $seg_2 = substr($line, 37, 99) !the rest of line
let $eleven_spaces = lpad(' ', 11, ' ')
string 'D' $seg_1 $eleven_spaces $seg_2 by '' into $line
end-if
write {OUTFILE} from $line
end-while
close {INFILE}
close {OUTFILE}
end-procedure write_new_file
Harold Peacock wrote:
> I've just started working in SQR, and I've been "thrown into the fire"
> literally with my current project. :) I've spent most of the day
> scanning posts and reviewing sample code (both online and in print).
>
> Given:
> File Name: FLATtest.txt (straight flat file, no commas or ticks)
>
> Primary Mission:
> (1) Read/Scan File...
> (2) Where position 1 is blank, insert letter 'D' into position 1 and
> null positions 26 - 36.
>
> ***** ROUGH CODE SO FAR *****
>
> Begin-Procedure Read-AT-File
> !
> ! Open FLATtest.txt
> !
> open 'FLATtest.txt' as 1 for-reading record=78:vary
>
> Do-Read-AT-File
>
> if #end-file
> break ! End of file reached
> end-if
>
> ! Close FLATtest.txt
> close 1
>
> End-Procedure ! Read-AT-File
>
> Begin-Procedure Update-AT-File
> !
> ! Open FLATtest.txt
> !
> open 'FLATtest.txt' as 2 for-writing record=78:vary
>
> Do-Update-AT-File
>
> if position (+1) is null
> write 'D' into position (+1)
> write ' ' into position (+26)
> write ' ' into position (+27)
> write ' ' into position (+28)
> write ' ' into position (+29)
> write ' ' into position (+30)
> write ' ' into position (+31)
> write ' ' into position (+32)
> write ' ' into position (+33)
> write ' ' into position (+34)
> write ' ' into position (+35)
> write ' ' into position (+36)
> end-if
>
> while 2 ! loop until break
>
> if #end-file
> break ! End of file reached
> end-if
>
> ! Close FLATtest.txt
> close 2
>
> End-Procedure ! Update-AT-File
>
> ************* EOF ***********
>
> If Possible:
> (1) Save original file as FLATtest.orig
> (2) Save updated file as FLATtest.new
>
> NOTE: Please be reminded that I just started coding in SQR and I'm just
> getting used to how it works. My machine isn't even configured to
> compile yet, so I'm not even sure if the syntax so far is correct. I
> have the logic down, it's just translating it properly is my issue now.
>
> Any assistance would be greatly appreciated.
>
> Thanks,
> H. Peacock
> hpeacock@yahoo.com
>
> __________________________________
> Do you Yahoo!?
> The New Yahoo! Shopping - with improved product search
> http://shopping.yahoo.com
>
> _______________________________________________
> sqr-users mailing list
> sqr-users@sqrug.org
> http://www.sqrug.org/mailman/listinfo/sqr-users
>
>
_______________________________________________
sqr-users mailing list
sqr-users@sqrug.org
http://www.sqrug.org/mailman/listinfo/sqr-users