Fixed length records: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(4 intermediate revisions by 3 users not shown)
Line 727:
=={{header|jq}}==
 
To read the raw input and write out raw strings, the command-line options -R (raw input), -sr ("slurp"raw output), and -r (raw output)j options are necessary:
 
jq -RrsRrj -f program.jq infile.dat
 
program.jq:
 
Currently gojq does not include the `_nwise/1` filter provided by jq, and in any case jq's implementation is suboptimal,
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:
so we define `nwise` here so that it can be used for both array and string inputs. Notice that the inner helper function, `n`, has arity 0, allowing jq's tail-call optimization to work.
 
<syntaxhighlight lang="jq">def cut($n):
def nwise($n):
def c: if length==0 then empty else .[:$n] , (.[$n:] | c) end;
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
c;
n;
nwise(80) | explode | reverse | implode
</syntaxhighlight>
To show that this does indeed work, the output of `/bin/dd cbs=80 conv=unblock` is shown:
{{output}}
<pre>
8.........7.........6.........5.........4.........3.........2.........1...1 eniL
2 eniL
3 eniL
4 eniL
 
6 eniL
cut(80) | explode | reverse | implode;</syntaxhighlight>
7 eniL
............................................................8 enil detnednI
NIGRAM TR 9 eniL
1+1 records in
1+1 records out
644 bytes transferred in 0.030319 secs (21241 bytes/sec)
</pre>
 
=={{header|Julia}}==
Line 1,586 ⟶ 1,603:
............................................................8 enil detnednI
NIGRAM TR 9 eniL</pre>
 
=={{header|TXR}}==
 
===80 Column Task===
 
At the shell prompt:
 
<pre>
$ txr -e '(let ((buf (make-buf 80))
(nread 80))
(while (eql nread 80)
(set nread (fill-buf-adjust buf))
(buf-set-length buf 80)
(put-buf (nreverse buf))))' < infile80.bin > outfile80.bin
$ dd if=outfile80.bin cbs=80 conv=unblock
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
 
1+1 records in
1+1 records out
725 bytes copied, 8.658e-05 s, 8.4 MB/s
</pre>
 
This handles a final bit that is shorter than 80. When <code>buf-set-length</code> sets the buffer size back to 80, zero padding is applied. The zero-padded record is reversed.
 
===Forth Blocks Task===
 
====Encoding====
 
The following program is called <code>forth-enblock.tl</code>:
 
<syntaxhighlight lang="txrlisp">(typedef forth-line (array 64 bchar))
 
(let ((lno 0)
(blanks (make-buf 64 #\space)))
(whilet ((line (get-line)))
(put-obj (fmt "~-64,64a" line) (ffi forth-line))
(inc lno))
(while (plusp (mod (pinc lno) 16))
(put-buf blanks)))</syntaxhighlight>
 
{{out}}
 
<pre>$ txr forth-enblock.tl < forth-enblock.tl > forth-enblock.blk
$ xxd forth-enblock.blk
00000000: 2874 7970 6564 6566 2066 6f72 7468 2d6c (typedef forth-l
00000010: 696e 6520 2861 7272 6179 2036 3420 6263 ine (array 64 bc
00000020: 6861 7229 2920 2020 2020 2020 2020 2020 har))
00000030: 2020 2020 2020 2020 2020 2020 2020 2020
00000040: 2020 2020 2020 2020 2020 2020 2020 2020
00000050: 2020 2020 2020 2020 2020 2020 2020 2020
00000060: 2020 2020 2020 2020 2020 2020 2020 2020
00000070: 2020 2020 2020 2020 2020 2020 2020 2020
00000080: 286c 6574 2028 286c 6e6f 2030 2920 2020 (let ((lno 0)
[... snip ...]
000003e0: 2020 2020 2020 2020 2020 2020 2020 2020
000003f0: 2020 2020 2020 2020 2020 2020 2020 2020</pre>
 
====Decoding====
 
This is <code>forth-deblock.tl</code>
 
<syntaxhighlight lang="txrlisp">(typedef forth-block (array 16 (array 64 bchar)))
 
(defsymacro fbsz (sizeof forth-block))
 
(let* ((buf (make-buf fbsz))
(block-view (carray-buf buf (ffi forth-block)))
(nread fbsz))
(while (eql fbsz nread)
(set nread (fill-buf-adjust buf))
(when (plusp nread)
(buf-set-length buf fbsz #\space)
(each ((row [block-view 0]))
(put-line (trim-right #/ +/ row))))))</syntaxhighlight>
 
{{out}}
 
<pre>$ txr forth-deblock.tl < forth-enblock.blk
(typedef forth-line (array 64 bchar))
 
(let ((lno 0)
(blanks (make-buf 64 #\space)))
(whilet ((line (get-line)))
(put-obj (fmt "~-64,64a" line) (ffi forth-line))
(inc lno))
(while (plusp (mod (pinc lno) 16))
(put-buf blanks)))
 
 
 
 
 
 
 
</pre>
 
=={{header|Wren}}==
{{libheader|Wren-str}}
<syntaxhighlight lang="ecmascriptwren">import "io" for File
import "./str" for Str
 
var records = File.read("infile.dat")
Line 1,617 ⟶ 1,737:
===Bonus round===
{{libheader|Wren-ioutil}}
<syntaxhighlight lang="ecmascriptwren">import "./ioutil" for File, FileUtil
import "./str" for Str
 
var blockToText = Fn.new { |blockFileName, textFileName|
Line 1,653 ⟶ 1,773:
<pre>
true
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang "XPL0">include xpllib;
int N, I;
char A(80);
[OpenInFile("infile.dat");
OpenOutFile("outfile.dat");
for N:= 1 to 9 do
[for I:= 0 to 80-1 do A(I):= ChIn(3);
for I:= 80-1 downto 0 do ChOut(3, A(I));
];
OpenInFile("outfile.dat");
for N:= 1 to 9 do
[for I:= 0 to 80-1 do ChOut(0, ChIn(3)); CrLf(0)];
]</syntaxhighlight>
{{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>
 
9,488

edits