Write float arrays to a text file: Difference between revisions
→{{header|Perl 6}}: GLR fix, improved idiomatic version
SqrtNegInf (talk | contribs) (→{{header|Perl 6}}: GLR fix, improved idiomatic version) |
|||
Line 1,133:
=={{header|Perl 6}}==
===Perl 5-ish===
Written in the style of the 2nd Perl 5 example.
<lang perl6>sub
my $fh = open $filename, :w;
for flat @x Z @y -> $x, $y {▼
▲ for @x Z @y -> $x, $y {
$fh.printf: "%.*g\t%.*g\n", $x_precision, $x, $y_precision, $y;
}
$fh.close;
}
Line 1,147 ⟶ 1,146:
my @y = @x.map({.sqrt});
{{out}}
<pre>1 1
2 1.4142
3 1.7321
1e+11 3.1623e+05</pre>
===Idiomatic===
Written in a more idiomatic style.
▲<lang perl6>sub writefloat($filename, @x, @y, :$x-precision = 3, :$y-precision = 5) {
join '', flat (@x>>.
▲ open($filename, :w).print(
▲ flat @x>>.base(10, $x-precision) Z TAB Z @y>>.base(10, $y-precision) Z NL);
}
my @x = 1, 2, 3, 1e11;
writefloat('sqrt.dat', @x, @x>>.sqrt, :y-precision(
{{out}}
<pre>1 1
2 1.4142
3 1.7321
1e+11 3.1623e+05</pre>
=={{header|Phix}}==
|