number format in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting number format in Perl
# 1  
Old 02-13-2008
number format in Perl

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,
# 2  
Old 02-13-2008
use sprintf or printf:

Code:
my $n = 10;
for (1 .. 100) {
   $n += .4;
   printf "%.1f\n", $n
}

# 3  
Old 02-13-2008
but I need show text1 ~ text100 in output file, the increased number be added at the end of each text.
# 4  
Old 02-13-2008
my example is a helping hand, not a complete solution. But what you want to do should be very easy, give it try.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl module to convert xlsx format to xls format

Hi Folks, I have written a perl script that reads data from excel sheet(.xls) using Spreadsheet::ParseExcel module. But the problem is this module doesn't work for excel sheets with extension .xlsx. I have gone through Spreadsheet::XLSX module with which we can read from .xlsx file directly.... (1 Reply)
Discussion started by: giridhar276
1 Replies

2. Shell Programming and Scripting

Perl : print the sequence number without missing number

Dear Perl users, I need your help to solve my problem below. I want to print the sequence number without missing number within the range. E.g. my sequence number : 1 2 3 4 5 6 7 8 11 12 13 14 my desired output: 1 -8 , 11-14 my code below but still problem with the result: 1 - 14 1 -... (2 Replies)
Discussion started by: mandai
2 Replies

3. Shell Programming and Scripting

how to format a number in bash

Say I have a number x=123, but I want to it be x=000123, because I need to use it in as a file name. thanks! (2 Replies)
Discussion started by: aerosols
2 Replies

4. Shell Programming and Scripting

awk:How to format a number?

Hello, I need to format a number..like 12900 should be printed as 12,900 and 1209 as 1,209 and so on. (Just like we do in excel). Can this be done in awk. any printf options we have?Please suggest me. Thanks! (8 Replies)
Discussion started by: vijay_0209
8 Replies

5. Shell Programming and Scripting

Need to change format of number

Hi, using a shell script to get values from a CSV eg: 12345.67,5678990.89,76232882.90 12345,5678990.89,76232882 Need the format of these numbers to change to 12,345.67:5,678,990.89:76,232,882.90 12,345:5678990.89:76232882 Using nawk on solaris, to parse these values, need the... (10 Replies)
Discussion started by: pgop
10 Replies

6. Shell Programming and Scripting

Number/string format in bash

I would like to change the format of an integer type number adding zeros to the left of it in a script in bash. For example number=1 echo $number 00001 Thanks (3 Replies)
Discussion started by: josegr
3 Replies

7. Web Development

Need Number Format Help in PHP

I have a number coming into a php echo statement that looks like 0293 and i want to output it looking like 29.3 and for the life of me i cannot figure out how to do it with available php functions like number_format, sprintf, or printf. Sample Data: 0293 0304 0282 0310 1324 2000 ... (2 Replies)
Discussion started by: RacerX
2 Replies

8. Shell Programming and Scripting

Number format conversion in UNIX

Hi All, I want to convert Scientific number format into normal number format using unix script or using any command of unix. e.g 1.55397e+09 into 1553970000 Thanks in advance Kamal (1 Reply)
Discussion started by: kamal_418
1 Replies

9. Shell Programming and Scripting

Help with format a number in a file

Hey, I have a file which starts each line with 6 digits followed bya colon: 090607:The rest of the line 091207:Also some text 091207:Here's some more text And I want to reformat them into: 06-09-07:The rest of the line 12-09-07:Also some text 12-09-07:Here's some more text I... (3 Replies)
Discussion started by: kabatsie
3 Replies

10. Shell Programming and Scripting

How to format number/string in ksh

Hi, How to format a number in ksh. For example x=RANDOM $$ I want x to be of 20 digits long, so if x = 12345 I want it to be left paded with 15 zeros. Thanks. (2 Replies)
Discussion started by: GNMIKE
2 Replies
Login or Register to Ask a Question