I try to read in a file and write out a new file with increased number at the end of each line. And I can set the initial value and increased constant from inputs.
input file:
text1
text2
text3
...
text100
if I set initial value is 10, and increased constant is 0.4
output file:
text1 10
text2 10.4
text3 10.8
...
text100 49.6
Code:
if ($#ARGV != 3) {
print "usage: do inputfile initial step outputfile\n";
exit;
}
$infile = $ARGV[0];
$initial = $ARGV[1];
$cons = $ARGV[2];
$outfile = $ARGV[3];
open(OF, $infile);
open(NF, ">$outfile");
my $offset = $initial;
# read in each line of the file
while ($line = <OF>) {
$line =~ s/$/ $offset/;
$offset += $cons;
print NF $line;
}
close(OF);
close(NF);
But my output like:
text1 10
text2 10.4
text3 10.8
...
text54 31.1999999999999
text55 31.5999999999999
text56 31.9999999999999
text57 32.3999999999999
text58 32.7999999999999
...
text100 49.5999999999999
How to keep 1 decimal digit output? And why get this result?
Thanks,