Selective file copy: Difference between revisions
m
→{{header|Wren}}: Minor tidy
(→{{header|Go}}: Add Fortran.) |
m (→{{header|Wren}}: Minor tidy) |
||
(33 intermediate revisions by 16 users not shown) | |||
Line 1:
{{draft task}}
{{clarify task}}
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 ''records'' (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'' in the Go listing).
<br><br>
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Using formatted transput to process similar files to the COBOL sample.
<syntaxhighlight lang="algol68">MODE INPUTRECORD = STRUCT( STRING field a, STRING field b, INT field c, CHAR sign of field c, STRING field d );
MODE OUTPUTRECORD = STRUCT( STRING field a, INT field c, STRING field x );
FORMAT input format = $ 5a, 5a, 4d, 1a, 5a l $;
FORMAT output format = $ 5a, 3z-d, 5a l $;
IF FILE input file;
STRING file name = "input.txt";
open( input file, file name, stand in channel ) /= 0
THEN
# failed to open the file #
print( ( "Unable to open """ + file name + """", newline ) )
ELIF FILE output file;
STRING output name = "output.txt";
open( output file, output name, stand out channel ) /= 0
THEN
# failed to open the otput file #
print( ( "Unable to open """ + output name + """", newline ) );
close( input file )
ELSE
# files opened OK #
BOOL at eof := FALSE;
# set the EOF handler for the file #
on logical file end( input file, ( REF FILE f )BOOL:
BEGIN
# note that we reached EOF on the #
# latest read #
at eof := TRUE;
# return TRUE so processing can continue #
TRUE
END
);
WHILE INPUTRECORD in record;
getf( input file, ( input format, in record ) );
NOT at eof
DO
IF sign of field c OF in record = "-" THEN field c OF in record *:= -1 FI;
OUTPUTRECORD out record;
field a OF out record := field a OF in record;
field c OF out record := field c OF in record;
field x OF out record := "XXXXX";
putf( output file, ( output format, out record ) )
OD;
# close the file #
close( output file );
close( input file )
FI</syntaxhighlight>
Input file:
<pre>
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
</pre>
{{out}}
<pre>
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
=={{header|AWK}}==
{{works with|gawk}}
<!-- http://ideone.com/oPMkNK -->
There is no delimiter between input-fields, so we extract the fields from the whole input-record $0 as substrings using substr(). <br>
Output and formatting is done using printf().
<syntaxhighlight lang="awk"># usage: gawk -f sfcopy.awk input.txt
BEGIN {
print "# Selective File Copy:"
}
{
# print NR ": <" $0 ">" ## debug: print input
F1 = substr($0, 1,5)
F2 = substr($0, 11,4)
F3 = substr($0, 15,1)
if(F3=="-") {F2 = 0-F2}
F9 = "xxxxx"
printf("%-5s%5d%s\n", F1, F2, F9)
}
END {
print "# Done."
}
</syntaxhighlight>
{{out}}
<pre>
# Selective File Copy:
A 1xxxxx
AA 2xxxxx
AAA 3xxxxx
AAAA -1xxxxx
AAAAA -2xxxxx
# Done.
</pre>
=={{header|COBOL}}==
Line 52 ⟶ 171:
levels. In this example only levels 01, and 05 are used.
<syntaxhighlight lang="cobol">
01 ws-input-record.
:INPUT-RECORD:
</
<syntaxhighlight lang="cobol">
01 ws-input-record.
05 field-a pic x(5).
Line 63 ⟶ 182:
05 field-c pic s9(5).
05 field-d pic x(5).
</
And finally, the example selective-copy.cob
<syntaxhighlight lang="cobol">
*> Tectonics:
*> cobc -xj selective-copy.cob
Line 225 ⟶ 344:
.
end program selective-copy.</
The output file has no newlines, so normal '''cat''' type commands are not of
Line 272 ⟶ 391:
=={{header|Fortran}}==
In principle the contents of a file can be arbitrarily complex, with the structure really only comprehensible to the program that writes (and maybe reads) it especially if numbers are written in binary format whereby any bit sequence may appear - just consider the content of a "database" file. Thus, a separate program that selectively reads some data from such a file is faced with an arbitrarily difficult problem. However, if what is written is in text format only, various possible conventions might be followed, and in particular, Fortran offers the NAMELIST protocol, as exemplified below:<
TYPE PLACE
INTEGER N
Line 309 ⟶ 428:
IS.LATLONG = HERE.LATLONG !Piecemeal, as no common structure was defined.
WRITE (6,*) IS !Reveal the result.
END</
This produces a file in a standard format. All output starts with a space in column one (in case this output were to be sent to a lineprinter, for which that would be the carriage control character), and begins the block of output with a special line that gives the name of the NAMELIST prefixed with an ampersand, and ends it with a slash. Each line starts with the name of a variable followed by an equals, then comes its value in
<pre>
&STUFF
Line 325 ⟶ 444:
/
</pre>
This is suitable for input also. Variables can be named in any order,
If input is being obtained live from the keyboard, some Fortrans allow you to enter " ?" (note the leading space) to be advised of the name of the NAMELIST being sought and " =?" will list the current values of the NAMELIST collection, after which you may make your selection...
This style of input is useful when a large number of different parameters are to be read in, and on a given occasion, many of them may be omitted. It is thus more flexible than having some defined format for all of them, or, reading them all in free-format. Good mnemonics for the names will be very helpful.
To revert to the specific task, obviously the input could contain only values for variables of interest, or, if values for all variables are supplied (as in the example), after the READ only those of interest would be copied to other variables to be going on with. In the example, because Fortran offers no <code>IS = HERE, BY NAME</code> facility so that only the elements of HERE that have the same name as elements of IS (in perhaps a different order) will be copied (as in pl/i and others), the copy must be done element-by-element. If however the desired elements had been grouped into a substructure (say, called LOCATION) and both type PLACE and type HOME had used that substructure, then a single assignment could have been used, as when HERE is copied to THERE.
Output:<pre> Mt. Cook trig. (-41.29980,174.7763)</pre>
=={{header|FreeBASIC}}==
{{trans|Julia}}
<syntaxhighlight lang="freebasic">Open "m:\in.txt" For Input As #1
Open "m:\out.txt" For Output As #2
Dim As String linea, campoa, campob, campod
Dim As Integer campoc
Do Until Eof(1)
Line Input #1, linea
campoa = Left(linea, 5)
campob = Mid(linea,6,10)
campoc = Valint(Mid(linea,11,4)) * Iif(Mid(linea,15,1) = "-", -1, 1)
campod = Mid(linea,16,20)
Print #2, campoa; campoc; String(Len(campod), "X")
Loop
Close #1, #2
Sleep</syntaxhighlight>
{{out}}<pre>in.txt:
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
out.txt:
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA-2XXXXX
</pre>
=={{header|Go}}==
JSON is popular these days for structured data and Go has support for this kind of selective copy in the JSON reader. The reader also supports custom conversions on fields. The common idiom for record construction and field initialization is shown.
<
import (
Line 420 ⟶ 575:
}
}
}</
{{out|o1.json}}
<pre>
Line 437 ⟶ 592:
{"A":"AAAAA","C":"5","X":"XXXXX"}
</pre>
=={{header|J}}==
Here's some exposition, roughly imitating the [[#COBOL|COBOL]] implementation.
(Remember: J's prompt is three spaces, so indented lines here are J sentences, and the following line(s) is/are the result(s) from the sentence.)
First, we'll build the file and write it:<syntaxhighlight lang="j"> N=: {{(}.":1e4+y),'+-'{~3<y}}
R=: {{5{.y#x}}
('A'&R,'B'&R,N,'D'&R)&>1+i.5
A B 0001+D
AA BB 0002+DD
AAA BBB 0003+DDD
AAAA BBBB 0004-DDDD
AAAAABBBBB0005-DDDDD
(('A'&R,'B'&R,N,'D'&R)&>1+i.5) fwrite 'example'
100</syntaxhighlight>
Note that we've written 100 characters here -- there's no newlines in this file:
<syntaxhighlight lang="j"> fread'example'
A B 0001+D AA BB 0002+DD AAA BBB 0003+DDD AAAA BBBB 0004-DDDD AAAAABBBBB0005-DDDDD</syntaxhighlight>
So we'll need to parse the file based on our knowledge of it -- 20 character lines, and four five character wide fields. In the spirit of the task, we'll code this up as if the fields could be arbitrary (different) widths. But, for conciseness, we'll not show the bit where we would add up the field widths to find the record width:<syntaxhighlight lang="j"> (##\)5 5 5 5
1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 4 4 4 4 4
_20 ((##\)5 5 5 5)&(</.)\fread 'example'
┌─────┬─────┬─────┬─────┐
│A │B │0001+│D │
├─────┼─────┼─────┼─────┤
│AA │BB │0002+│DD │
├─────┼─────┼─────┼─────┤
│AAA │BBB │0003+│DDD │
├─────┼─────┼─────┼─────┤
│AAAA │BBBB │0004-│DDDD │
├─────┼─────┼─────┼─────┤
│AAAAA│BBBBB│0005-│DDDDD│
└─────┴─────┴─────┴─────┘</syntaxhighlight>
So that's the characters of the records from the file. We can assign these records to variables -- one for each column:<syntaxhighlight lang="j"> 'A B C D'=: |:_20 ((##\)5 5 5 5)&(</.)\fread 'example'
A NB. first column
A
AA
AAA
AAAA
AAAAA</syntaxhighlight>
Also, we want numeric values for that third column. Here's building up to that, and giving the numeric column variable a name:<syntaxhighlight lang="j"> C
0001+
0002+
0003+
0004-
0005-
_1|."1 C
+0001
+0002
+0003
-0004
-0005
0"._1|."1 C
1 2 3 _4 _5
,.0"._1|."1 C
1
2
3
_4
_5
N=: ,.0"._1|."1 C</syntaxhighlight>
Next, we'll want N as a five character wide column -- here, it makes sense to use J's " [https://www.jsoftware.com/help/dictionary/dx008.htm 8!:x format]" mechanism -- and we'll want a fill column: <syntaxhighlight lang="j"> '5.0' 8!:2 N
1
2
3
-4
-5
5#'X'
XXXXX</syntaxhighlight>
Finally, we'll glue the pieces back together and write that to a file:<syntaxhighlight lang="j"> A,"1('5.0' 8!:2,.N),"1(5#'X')
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -4XXXXX
AAAAA -5XXXXX
(A,"1('5.0' 8!:2,.N),"1(5#'X')) fwrite 'output'
75</syntaxhighlight>
Again, there's no newlines in the result file, so we would have to know something about its record structure to make sense of it:
<syntaxhighlight lang="text"> fread 'output'
A 1XXXXXAA 2XXXXXAAA 3XXXXXAAAA -4XXXXXAAAAA -5XXXXX
_15 ]\fread 'output'
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -4XXXXX
AAAAA -5XXXXX</syntaxhighlight>
=={{header|Java}}==
With a little help from my friens
<
import java.io.FileWriter;
import java.io.File;
Line 492 ⟶ 742:
return;
}
}</
=={{header|Julia}}==
<syntaxhighlight lang="julia">out = open("out.txt", "w")
for line in split(strip(read("in.txt", String)), "\n")
fielda, fieldb, fieldd = line[1:5], line[6:10], line[16:20]
fieldc = lpad((line[15] == '-' ? -1 : 1) * parse(Int, line[11:14]), 5)
print(out, fielda, fieldc, 'X'^length(fieldd), "\n")
end
</syntaxhighlight>{{out}}
<pre>
in.txt:
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
out.txt:
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.1.51
import java.io.File
fun process(line: String): String {
with (line) {
val a = substring(0, 5)
val n = (substring(14, 15) + substring(10, 14)).toInt()
return String.format("%s%5dXXXXX", a, n)
}
}
fun main(args: Array<String>) {
val out = File("selective_output.txt")
val pw = out.printWriter()
File("selective_input.txt").forEachLine { pw.println(process(it)) }
pw.close()
// check it worked
println(out.readText())
}</syntaxhighlight>
Contents of selective_input.txt:
<pre>
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
</pre>
{{out}}
<pre>
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
=={{header|NetRexx}}==
with a little help from a friend
<
-- nrc -keepasjava -savelog copys
options replace format comments java crossref symbols nobinary
Line 528 ⟶ 842:
ex.printStackTrace()
end
end</
=={{header|Nim}}==
{{trans|Phix}}
{{trans|Kotlin}}
<syntaxhighlight lang="nim">import strformat, strutils
const Data = """
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
"""
"in.txt".writeFile(Data)
let outfile = "out.txt".open(fmWrite)
for line in "in.txt".lines:
let a = line.substr(0, 4)
let n = parseInt(line[14] & line.substr(10, 13))
let s = &"{a}{n:5}XXXXX\n"
stdout.write s
outfile.write s
outfile.close()</syntaxhighlight>
{{out}}
<pre>A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX</pre>
=={{header|ooRexx}}==
<
infile ="in.txt"
outfile="out.txt"
Line 565 ⟶ 911:
expose instream outstream
instream~close
outstream~close</
{{out}}
<pre>AA 01XXXXX
Line 571 ⟶ 917:
AAAA 03XXXXX
AAAAA04XXXXX
AAAAA05XXXXX</pre>
=={{header|Perl}}==
{{trans|Raku}}
<syntaxhighlight lang="perl">my %F = ( # arbitrary and made up record format
'field a' => { offset => 0, length => 5, type => 'Str' },
'field b' => { offset => 5, length => 5, type => 'Str' },
'field c' => { offset => 10, length => 4, type => 'Bit' },
'field d' => { offset => 14, length => 1, type => 'Str' },
'field e' => { offset => 15, length => 5, type => 'Str' }
);
$record_length += $F{$_}{'length'} for keys %F;
open $fh, '<', 'sfc.dat' || die;
while ($n=sysread($fh, $record, $record_length)) {
last if $n < $record_length;
for $k (sort keys %F) {
if ($F{$k}{type} eq 'Str') {
printf "$k : %s ", $v = substr $record, $F{$k}{offset}, $F{$k}{length};
$h{$k} = $v;
} elsif ($F{$k}{type} eq 'Bit') {
printf "$k : %d ", $v = substr $record, $F{$k}{offset}, $F{$k}{length};
$h{$k} = pack("B8",'0011'.$v);;
}
}
print "\n";
push @result, sprintf( "%-5s%s%01d%5s", $h{'field a'}, $h{'field d'}, $h{'field c'}, 'xxxxx' );
}
print "\n" . join "\n", @result;
</syntaxhighlight>
{{out}}
<pre>field a : A field b : bbbbB field c : 1 field d : + field e : d2345
field a : A field b : bbbbB field c : 0001 field d : + field e : d2345
field a : AA field b : bbbBB field c : 0010 field d : + field e : 1d345
field a : AAA field b : bbBBB field c : 0011 field d : + field e : 12d45
field a : AAAA field b : bBBBB field c : 0100 field d : - field e : 123d5
field a : AAAAA field b : BBBBB field c : 0101 field d : - field e : 1234d
A +1xxxxx
AA +2xxxxx
AAA +3xxxxx
AAAA -4xxxxx
AAAAA-5xxxxx</pre>
=={{header|Phix}}==
{{trans|Kotlin}}
Obviously it does not perform any actual file i/o under pwa/pj2s and merely shows the same screen output.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
"""</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fin</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">fout</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"in.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fin</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"in.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"r"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fout</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"out.txt"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">else</span>
<span style="color: #000000;">data</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">fin</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">line</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">gets</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #004080;">atom</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">fin</span><span style="color: #0000FF;">></span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">data</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">line</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">[</span><span style="color: #000000;">fin</span><span style="color: #0000FF;">]</span>
<span style="color: #000000;">fin</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">15</span><span style="color: #0000FF;">]&</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">11</span><span style="color: #0000FF;">..</span><span style="color: #000000;">14</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">?</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]:</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%s%5dXXXXX\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">line</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">5</span><span style="color: #0000FF;">],</span><span style="color: #000000;">d</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fout</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fin</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fout</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
=={{header|PicoLisp}}==
<syntaxhighlight lang="picolisp">(de record (F)
(in F
(until (eof)
(tab (5 5 5)
(make (do 5 (link (char))))
(and
(do 5
(char) )
(till "+-")
(format (conc (cons (peek)) @)) )
"XXXXX" )
(line) ) ) )
(record "selective.in")</syntaxhighlight>
{{out}}
<pre>A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX</pre>
=={{header|PL/I}}==
<
copys: Proc Options(Main);
Dcl 1 s1 unal,
Line 605 ⟶ 1,072:
eoj:
End;
</syntaxhighlight>
{{out}}
<pre>AA 01XXXXX
Line 612 ⟶ 1,079:
AAAAA04XXXXX
AAAAA05XXXXX</pre>
=={{header|Racket}}==
{{trans|Kotlin}}
<syntaxhighlight lang="racket">#lang racket
(define (read+write)
(for ([line (in-lines)])
(define a (substring line 0 5))
(define n (string->number (string-append (substring line 14 15)
(substring line 10 14))))
(printf "~a~aXXXXX\n" a (~a n #:min-width 5 #:align 'right))))
(with-output-to-file "selective-output.txt" #:mode 'text #:exists 'replace
(thunk (with-input-from-file "selective-input.txt" read+write)))</syntaxhighlight>
{{in}}
<pre>
A bbbbB0001+d2345
AA bbbBB0002+1d345
AAA bbBBB0003+12d45
AAAA bBBBB0001-123d5
AAAAABBBBB0002-1234d
</pre>
{{out}}
<pre>
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.10}}
I have no idea how PL/I or COBOL store records and little enthusiasm to research it. If the task author can't be bothered to spell out what the format should look like, then I have no compunction about just making something up out of whole cloth. In the absence of better guidance I am going to make a binary encoded data file format with fixed sized fields consisting of a mix of ISO-8859-1 encoded text and raw binary (hex) encoded integers.
This is WAY more complicated than it could be. Could achieve the same effect in one or two lines of code, but this explicitly shows some of the possible mechanics.
Since the sfc.dat file is binary encoded, I can't include it here easily as text so [https://github.com/thundergnat/rc/blob/master/sfc.dat here is a link to an online copy] instead.
<syntaxhighlight lang="raku" line>my @format = ( # arbitrary and made up record format
'field a' => { offset => 0, length => 5, type => 'Str' },
'field b' => { offset => 5, length => 5, type => 'Str' },
'field c' => { offset => 10, length => 4, type => 'Int' },
'field d' => { offset => 14, length => 1, type => 'Str' },
'field e' => { offset => 15, length => 5, type => 'Str' }
);
my $record-length = @format[*]».value».<length>.sum;
my $in = './sfc.dat'.IO.open :r :bin;
say "Input data as read from $in:";
my @records;
@records.push: get-record($in, $record-length) until $in.eof;
.perl.say for @records;
# not going to bother to actually write out to a file, if you really want to,
# supply a file handle to a local file
say "\nOutput:";
my $outfile = $*OUT; # or some other filename, whatever.
for @records -> $r {
$outfile.printf( "%-5s%s%08x%5s\n", flat $r.{'field a','field d','field c'}, 'xxxxx' );
}
sub get-record($fh, $bytes) {
my $record = $fh.read($bytes);
return ().Slip unless $record.elems == $bytes;
my %r = @format.map: {
.key => do given $_.value.<type> -> $type
{
when $type eq 'Str' { $record.subbuf($_.value.<offset>, $_.value.<length>).decode }
when $type eq 'Int' { sum $record.subbuf($_.value.<offset>, $_.value.<length>) Z+< (24,16,8,0) }
default { $record.subbuf($_.value.<offset>, $_.value.<length>) } # Buf
}
}
}</syntaxhighlight>
{{out}}
<pre>Input data as read from ./sfc.dat:
${"field a" => "A ", "field b" => "bbbbB", "field c" => 1, "field d" => "+", "field e" => "d2345"}
${"field a" => "AA ", "field b" => "bbbBB", "field c" => 2, "field d" => "+", "field e" => "1d345"}
${"field a" => "AAA ", "field b" => "bbBBB", "field c" => 3, "field d" => "+", "field e" => "12d45"}
${"field a" => "AAAA ", "field b" => "bBBBB", "field c" => 4, "field d" => "-", "field e" => "123d5"}
${"field a" => "AAAAA", "field b" => "BBBBB", "field c" => 3729368837, "field d" => "-", "field e" => "1234d"}
Output:
A +00000001xxxxx
AA +00000002xxxxx
AAA +00000003xxxxx
AAAA -00000004xxxxx
AAAAA-de49a705xxxxx</pre>
=={{header|REXX}}==
{{trans|PL/I}}
<
out='out.txt'; 'erase' out
Do While lines(in)>0
Line 626 ⟶ 1,188:
Call lineout in
Call lineout out
'type' out</
{{out}}
Using the test file produced by PL/I.
Line 635 ⟶ 1,197:
AAAAA04XXXXX
AAAAA05XXXXX</pre>
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "io" for File
import "./ioutil" for FileUtil
import "./fmt" for Fmt
var process = Fn.new { |line|
var a = line[0...5]
var n = Num.fromString(line[14] + line[10...14])
return Fmt.swrite("$s$5dXXXXX", a, n)
}
var lb = FileUtil.lineBreak
File.create("selective_output.txt") { |f|
var first = true
File.read("selective_input.txt").trim().split(lb).each { |line|
if (!first) f.writeBytes(lb)
f.writeBytes(process.call(line))
if (first) first = false
}
}
// check it worked
System.print(File.read("selective_output.txt"))</syntaxhighlight>
{{out}}
<pre>
Same as Kotlin example.
</pre>
=={{header|zkl}}==
Taking a clue from ALGOL 68 and Awk.
<syntaxhighlight lang="zkl">File("in.txt").pump(File("out.txt","w"),fcn(line){
// 012345678901234567890...
// A bbbbB0001+d2345
// fields:1 2 3 45, field 4 is +|-
fld1,fld3,fld4,fld5 := line[0,5], line[10,4],line[14], line[15,*].strip();
if(fld4=="-") fld3=-fld3.toInt();
"%-5s%5d%s\n".fmt(fld1,fld3,"X"*fld5.len())
})</syntaxhighlight>
{{out}}
<pre>
$ zkl bbb
$ cat out.txt
A 1XXXXX
AA 2XXXXX
AAA 3XXXXX
AAAA -1XXXXX
AAAAA -2XXXXX
</pre>
|