Fixed length records: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(REBOL version)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 98:
NIGRAM TR 9 eniL
</pre>
 
=={{header|COBOL}}==
 
Line 396 ⟶ 397:
=={{header|Free Pascal}}==
''See [[#Pascal|Pascal]]''
 
=={{header|J}}==
'''Solution:'''
 
Using the 720 byte input file linked to in the Perl6 entry.
<lang j> _80 ]\ fread 'flr-infile.dat' NB. reads the file into a n by 80 array
_80 |.\ fread 'flr-infile.dat' NB. as above but reverses each 80 byte chunk
'flr-outfile.dat' fwrite~ , _80 |.\ fread 'flr-infile.dat' NB. as above but writes result to file (720 bytes)
processFixLenFile=: fwrite~ [: , _80 |.\ fread NB. represent operation as a verb/function</lang>
 
'''Example Usage:'''
 
<lang j> 'flr-outfile.dat' processFixLenFile 'flr-infile.dat' NB. returns number of bytes written
720</lang>
 
=={{header|jq}}==
 
To read the raw input and write out raw strings, the command-line options -R (raw input), -s ("slurp"), and -r (raw output) options are necessary:
 
jq -Rrs -f program.jq infile.dat
 
program.jq:
 
For the sake of generality, we define `cut` with an argument corresponding to the number of columns. To illustrate that an efficient recursive definition is possible, we define an inner helper function, `c`, with arity 0, as currently jq's tail-call optimization is restricted to zero-arity filters:
 
<lang jq>def cut($n):
def c: if length==0 then empty else .[:$n] , (.[$n:] | c) end;
c;
 
cut(80) | explode | reverse | implode;</lang>
 
=={{header|Julia}}==
The program reads from an "infile.dat" file created with dd, as described in the task instructions.
<lang julia>
function processfixedlengthrecords(infname, blocksize, outfname)
inf = open(infname)
outf = open(outfname, "w")
filedata = [ read(inf, blocksize) for _ in 1:10 ]
for line in filedata
s = join([Char(c) for c in line], "")
@assert(length(s) == blocksize)
write(outf, s)
end
end
processfixedlengthrecords("infile.dat", 80, "outfile.dat")
</lang>
 
=={{header|Go}}==
Line 591 ⟶ 544:
text2block("block.txt", "block2.dat")
}</lang>
 
=={{header|J}}==
'''Solution:'''
 
Using the 720 byte input file linked to in the Perl6 entry.
<lang j> _80 ]\ fread 'flr-infile.dat' NB. reads the file into a n by 80 array
_80 |.\ fread 'flr-infile.dat' NB. as above but reverses each 80 byte chunk
'flr-outfile.dat' fwrite~ , _80 |.\ fread 'flr-infile.dat' NB. as above but writes result to file (720 bytes)
processFixLenFile=: fwrite~ [: , _80 |.\ fread NB. represent operation as a verb/function</lang>
 
'''Example Usage:'''
 
<lang j> 'flr-outfile.dat' processFixLenFile 'flr-infile.dat' NB. returns number of bytes written
720</lang>
 
=={{header|jq}}==
 
To read the raw input and write out raw strings, the command-line options -R (raw input), -s ("slurp"), and -r (raw output) options are necessary:
 
jq -Rrs -f program.jq infile.dat
 
program.jq:
 
For the sake of generality, we define `cut` with an argument corresponding to the number of columns. To illustrate that an efficient recursive definition is possible, we define an inner helper function, `c`, with arity 0, as currently jq's tail-call optimization is restricted to zero-arity filters:
 
<lang jq>def cut($n):
def c: if length==0 then empty else .[:$n] , (.[$n:] | c) end;
c;
 
cut(80) | explode | reverse | implode;</lang>
 
=={{header|Julia}}==
The program reads from an "infile.dat" file created with dd, as described in the task instructions.
<lang julia>
function processfixedlengthrecords(infname, blocksize, outfname)
inf = open(infname)
outf = open(outfname, "w")
filedata = [ read(inf, blocksize) for _ in 1:10 ]
for line in filedata
s = join([Char(c) for c in line], "")
@assert(length(s) == blocksize)
write(outf, s)
end
end
processfixedlengthrecords("infile.dat", 80, "outfile.dat")
</lang>
 
=={{header|M2000 Interpreter}}==
Line 917 ⟶ 918:
and
[https://github.com/SqrtNegInf/Rosettacode-Perl5-Smoke/blob/master/ref/flr-outfile.dat output file]
 
=={{header|Perl 6}}==
Link to a copy of the input file used: [https://github.com/thundergnat/rc/blob/master/resouces/flr-infile.dat flr-infile.dat]
 
Essentially the same as task [[Selective_File_Copy]] except more boring.
 
<lang perl6>$*OUT = './flr-outfile.dat'.IO.open(:w, :bin) orelse .die; # open a file in binary mode for writing
while my $record = $*IN.read(80) { # read in fixed sized binary chunks
$*OUT.write: $record.=reverse; # write reversed records out to $outfile
$*ERR.say: $record.decode('ASCII'); # display decoded records on STDERR
}
close $*OUT;</lang>
{{out}}
<pre>8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
6 eniL
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL</pre>
 
=={{header|Phix}}==
Line 1,060 ⟶ 1,039:
"Line 9"}
</pre>
 
=={{header|Python}}==
<lang Python>
Line 1,091 ⟶ 1,071:
1+1 records out
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Link to a copy of the input file used: [https://github.com/thundergnat/rc/blob/master/resouces/flr-infile.dat flr-infile.dat]
 
Essentially the same as task [[Selective_File_Copy]] except more boring.
 
<lang perl6>$*OUT = './flr-outfile.dat'.IO.open(:w, :bin) orelse .die; # open a file in binary mode for writing
while my $record = $*IN.read(80) { # read in fixed sized binary chunks
$*OUT.write: $record.=reverse; # write reversed records out to $outfile
$*ERR.say: $record.decode('ASCII'); # display decoded records on STDERR
}
close $*OUT;</lang>
{{out}}
<pre>8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
6 eniL
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL</pre>
 
=={{header|REBOL}}==
Line 1,102 ⟶ 1,105:
close out
</lang>
 
 
=={{header|REXX}}==
10,333

edits