Re: converting to FLOATING_POINT..
- From: charley@xxxxxxxxxxxx (Chris Charley)
- Date: Wed, 17 Aug 2005 21:51:11 -0400
[...]
Hi Vineet
You could format *pretty* by using sprintf() instead of print. I would do it like below.
use strict; use warnings;
my $mdout_file = "mdout.txt";
my $mdout_xtemp_file = "temp.txt";
open IN, $mdout_file or die; open OUT, ">$mdout_xtemp_file" or die;
while (<IN>){
if (/TEMP/) {
my $time = (substr($_, 30, 14)); $time =~ s/\s//g; my $temp = (substr($_, 53, 10)); $temp =~ s/\s//g;
print OUT sprintf("%4.1f %6.2f\n", $time*2, $temp); } }
The solution above *assumes* that you would know beforehand the widths you want for each column (determined by the largest number in each column you want to format).
The solution below allows you to determine the greatest width required programmatically using the max() function from the List::Util module. Then, the max widths for each column are used in the sprintf function, (which uses a '*' as a placeholder, if thats an accurate term for the asterisk :-) ). This would provide a better solution (in place of 'hard coding' the widths in sprintf).
#!/usr/bin/perl use strict; use warnings; use List::Util qw/ max /;
my $mdout_file = "mdout.txt";
my $mdout_xtemp_file = "temp.txt";
open IN, $mdout_file or die;
my @time;
my @temp;
while (<IN>){ if (/TEMP/) { my $time = (substr($_, 30, 14));
$time =~ s/\s//g;
my $temp = (substr($_, 53, 10));
$temp =~ s/\s//g; push @time, $time*2;
push @temp, $temp;
}
}
close IN or die $!;my $maxlen_time = max map{ length } @time;
my $maxlen_temp = max map{ length } @temp;open OUT, ">$mdout_xtemp_file" or die;
for my $i (0..$#time) {
print OUT sprintf("%*.1f %*.2f\n",
$maxlen_time, $time[$i], $maxlen_temp, $temp[$i]);
}
close OUT or die $!;
Chris
.
- Follow-Ups:
- Re: converting to FLOATING_POINT..
- From: Chris Charley
- Re: converting to FLOATING_POINT..
- References:
- Re: converting to FLOATING_POINT..
- From: Rex Rex
- Re: converting to FLOATING_POINT..
- From: Vineet Pande
- Re: converting to FLOATING_POINT..
- From: Chris Charley
- Re: converting to FLOATING_POINT..
- Prev by Date: Re: perl app as a service ?
- Next by Date: Re: Perl Net::Pcap loop hang ??
- Previous by thread: Re: converting to FLOATING_POINT..
- Next by thread: Re: converting to FLOATING_POINT..
- Index(es):
Relevant Pages
|