Daniel Wilson wrote:

> Hello
> 
> I have a weird situation, it reminds me (many many years ago) of using
> COBOL and pic statements to align data.
> 
> I have to build a file containing the followingdata , to pass to GAMS
> (Mathematical Modeling Engine)
> 
>                      JPYUSD             EURUSD           GBPUSD    
> JPYUSD     1.000000000        0.02016       0.113887000
> EURUSD    0.020160000        1.000000000      -0.039177000
> GBPUSD    0.11388700       -0.0391770      1.000000000
> 
> The GAMS barfed, because the Column header are not aligned with the data
> under it (Exactly aligned, even the "-" has to be aligned) .   I could
> spend allot of time messing with the alignment, using padding and
> sprintf statements.
> 
> However, I just wanted to check to see if anyone knows a better way of
> handling this, maybe templates?
> 
> Your Thoughts?

Perl has a formatted write, but I don't like it.  I usually just make
up parallel tables of titles, field specifiers and variables and then
just do a sprintf in a foreach and it's that easy.

use strict;

my ($var1, $var2) = ('Some characters', 2);
my @titles = ('Column 1     ', 'Column2  ',);
my @fmts = ('%-12.12s ', '%08x ',);
my @vars = ($var1, $var2);

printf $titles[$_] foreach (0 .. scalar (@titles) - 1);
printf "\n";

# I like to build a prototype line and store inline just before the print

#          1         2         3         4         5         6         7
# 123456789012345678901234567890123456789012345678901234567890123456789012345678
# Column 1     Column2
# Some charact 00000002

foreach my $line (<STDIN>) {

        # parse line into vars

        printf $fmts[$_], $vars[$_] foreach (0 .. scalar (@vars) - 1);
        printf "\n";
}

__END__


-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:[EMAIL PROTECTED]
 (_/   /  )    // //       DBE Collectibles    Mailto:[EMAIL PROTECTED]
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
ActivePerl mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to