Re: converting to FLOATING_POINT..



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



.



Relevant Pages

  • Re: converting to FLOATING_POINT..
    ... 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 ... push @temp, $temp; # incorrect ... The format should be applied at this point so that that when the max_length is calculated below, the width of each $time and $temp variable will be as it will be when printed out. ...
    (perl.beginners)
  • Re: Adding a new file extension for Pipe Delimited files
    ... Dim Temp As String ... Dim FileNumber As Long ... open and handle like CSV files? ... allow me to keep the pipe delimited format. ...
    (microsoft.public.excel.programming)
  • traslation
    ... align 16, db 0 ... /* numero nello stack del chiamante in num1:num0 numero ... /* convert the number in format above in the 0 terminated ...
    (alt.lang.asm)
  • Temp Database problems with Access 2007
    ... I've been using Tony Toew's Temp table module and I've now upgraded to ... The database is still in 2003 format. ... ' This subroutine illustrates how to use a temporary MDB in your app. ...
    (comp.databases.ms-access)
  • Re: alignment with decimals & % sign
    ... lines) in the column with a custom format such as 0_% assuming your ... I cannot align columns of numbers the way I want. ... In Excel, I cannot find an align on decimal, ... can do that is to use the underscore and color it white.) ...
    (microsoft.public.excel.misc)