Selective file copy: Difference between revisions
Walterpachl (talk | contribs) (Expand task description) |
Walterpachl (talk | contribs) (add ooRexx) |
||
Line 6: | Line 6: | ||
<br>The ''by name'' assignment is a little extra available in PL/I. |
<br>The ''by name'' assignment is a little extra available in PL/I. |
||
<br>Data conversions may be necessary (as shown here for data element ''c''. |
<br>Data conversions may be necessary (as shown here for data element ''c''. |
||
=={{header|ooRexx}}== |
|||
<lang oorexx>/* REXX */ |
|||
infile ="in.txt" |
|||
outfile="out.txt" |
|||
s1=.copys~new(infile,outfile) |
|||
loop i=1 to 5 |
|||
s1~~input~~output |
|||
end |
|||
s1~close -- close streams (files) |
|||
'type' outfile |
|||
::class copys |
|||
::attribute a |
|||
::attribute b |
|||
::attribute c |
|||
::attribute d |
|||
::method init -- constructor |
|||
expose instream outstream |
|||
parse arg infile, outfile |
|||
instream =.stream~new(infile)~~open |
|||
outstream=.stream~new(outfile)~~open("replace") |
|||
::method input -- read an input line |
|||
expose instream a b c d |
|||
parse value instream~linein with a +5 b +5 c +5 d +5 |
|||
::method output -- write an output line |
|||
expose outstream a c |
|||
outstream~lineout(a || c~c2x~left(2)'XXXXX') |
|||
::method close -- close files |
|||
expose instream outstream |
|||
instream~close |
|||
outstream~close</lang> |
|||
{{out}} |
|||
<pre>AA 01XXXXX |
|||
AAA 02XXXXX |
|||
AAAA 03XXXXX |
|||
AAAAA04XXXXX |
|||
AAAAA05XXXXX</pre> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
Revision as of 07:09, 4 May 2015
Copy part of input records to an output file.
Show how file processing as known from PL/I or COBOL can be implemented in the
language of your choice.
Here, a file is not 'just' a sequence of bytes or lines but a sequence of recods (structured data). The structure is usually described by declarations contained in an INCLUDE file (PL/I) or COPY BOOK (COBOL).
The by name assignment is a little extra available in PL/I.
Data conversions may be necessary (as shown here for data element c.
ooRexx
<lang oorexx>/* REXX */ infile ="in.txt" outfile="out.txt"
s1=.copys~new(infile,outfile) loop i=1 to 5
s1~~input~~output
end s1~close -- close streams (files) 'type' outfile
- class copys
- attribute a
- attribute b
- attribute c
- attribute d
- method init -- constructor
expose instream outstream parse arg infile, outfile instream =.stream~new(infile)~~open outstream=.stream~new(outfile)~~open("replace")
- method input -- read an input line
expose instream a b c d parse value instream~linein with a +5 b +5 c +5 d +5
- method output -- write an output line
expose outstream a c outstream~lineout(a || c~c2x~left(2)'XXXXX')
- method close -- close files
expose instream outstream instream~close outstream~close</lang>
- Output:
AA 01XXXXX AAA 02XXXXX AAAA 03XXXXX AAAAA04XXXXX AAAAA05XXXXX
PL/I
<lang pli>*process source attributes xref or(!);
copys: Proc Options(Main); Dcl 1 s1 unal, 2 a Char(5), 2 b Char(5), 2 c Bin Fixed(31), 2 d Char(5); Dcl 1 s2, 2 a Char(5), 2 c Pic'99', 2 x Char(5) Init('XXXXX'); Dcl o1 Record Output; /* create a test file */ Dcl in Record Input; Dcl out Record Output; Do i=1 To 5; s1.a=repeat('A',i); s1.b=repeat('B',i); s1.c=i; s1.d=repeat('D',i); Write File(o1) From(s1); End; Close File(o1);
On Endfile(in) Goto eoj; Do i=1 By 1; /* copy parts of the test file */ Read File(in) Into(s1); s2=s1, by name; /* only fields a and c are copied */ Write File(out) From(s2); End; eoj: End;
</lang>
- Output:
AA 01XXXXX AAA 02XXXXX AAAA 03XXXXX AAAAA04XXXXX AAAAA05XXXXX
Rexx
<lang rexx>in='in.txt' out='out.txt'; 'erase' out Do While lines(in)>0
l=linein(in) Parse Var l a +5 b +5 c +4 d +5 chex=c2x(c) cpic=left(chex,2) call lineout out,a||cpic||'XXXXX' End
Call lineout in Call lineout out 'type' out</lang>
- Output:
Using the test file produced by PL/I. The data conversion used for c is not very general!
AA 01XXXXX AAA 02XXXXX AAAA 03XXXXX AAAAA04XXXXX AAAAA05XXXXX