Vineet Pande wrote:

Thanks Rex:
Please help me in knowing one more related thing. I have from this script of mine an output like this:
0.0    0.00
0.4    60.37
0.8    106.29
1.2    140.56
1.6    168.75
2.0    186.37
2.4    207.82
2.8    225.45
3.2    235.88
3.6    245.55
4.0    250.61
4.4    260.06
4.8    264.60
5.2    271.11
5.6    272.90
6.0    275.62
6.4    283.33
6.8    283.55
7.2    284.58
7.6    285.22
8.0    287.51
8.4    290.38
8.8    294.09
9.2    297.01
9.6    296.16
10.0    291.57
10.4    292.88
10.8    297.28
11.2    301.13

I want to see it formatted more beautifully, i.e. decimals under decimals. How do we get that. My script is:

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;

     $time = sprintf("%0.1f", ($time * 2));


                            foreach ($time)
                              {
                            print OUT $time ;
                            print OUT "    ";
                                    foreach ($temp) {
                                       print OUT $temp;
                                       print OUT "\n";
                                                    }
                              }

                            }

                      }


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);
  }
}

__END__

You should see the documentation;    perldoc -f sprintf
and this will help you understand the sprintf function better.

Also, I took out the foreach loops in your print routine. They really do nothing useful!

Rex was mistaken in saying a minus, '-', would right align. It left aligns :-)

HTH
Chris




--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to