Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks

I have the following snippet of code:

sub PRINT { printf("\n\t\t%-10s %-10s %-10s\n",'UAS','LCV','DM'); while(@UAS || @LCV || @DM) { printf("\t\t%-10s %-10s %-10s\n", shift(@UAS) || '', shift(@LCV) || '', shift(@DM) || '', ); } }
It produces:
UAS LCV DM 8 0 8 0 8 0 9 0 9 0 9 0 10 0 10 0 10 0 11 0 11 0 11 0 12 86400 12 0 12 0 13 0 13 0 13 0 14 0 14 0 14 0 15 0 15 0 15 0
How can I easily align the value column for each of the UAS, LCV, and DM?
i.e.
UAS LCV DM 8 0 8 0 8 0 9 0 9 0 9 0 10 0 10 0 10 0 11 0 11 0 11 0 12 86400 12 0 12 0 13 0 13 0 13 0 14 0 14 0 14 0 15 0 15 0 15 0
The left and right values are concatenated together in a previous step.
Thanks

Replies are listed 'Best First'.
Re: Printing format
by Hofmator (Curate) on Feb 08, 2003 at 16:58 UTC
    The left and right values are concatenated together in a previous step.
    I'll assume this is necessary for something else, otherwise it would be much easier if your arrays looked like this: @UAS = ( [8,0], [9,0], ...);

    But with the concated elements, I only see the general solution of splitting them up again, like this:

    sub PRINT { printf("\n\t\t%-10s %-10s %-10s\n",'UAS','LCV','DM'); while(@UAS || @LCV || @DM) { my @args = map { (defined) ? split(' ') : ('','') } shift(@UAS), shift(@LCV), shift(@DM); printf("\t\t" . '%3s %-5s ' x 3 . "\n", @args); } }

    -- Hofmator

Re: Printing format
by cees (Curate) on Feb 08, 2003 at 15:20 UTC

    You could use the perl format support for this. This is perl's support for simple report layouts. The docs do a good job of explaining how to use formats, so I won't try to rewrite them here.

Re: Printing format
by Anonymous Monk on Feb 08, 2003 at 18:34 UTC

    #! perl -slw use strict; sub PrintEm { my ($UAS, $LCV, $DM) = @_; print("\n UAS LCV DM"); while(@$UAS || @$LCV || @$DM) { printf "\t%5d %5d %5d %5d %5d %5d\n", map{ @$_ ? ( split ' ',shift @$_, 2 ) : (0,0) } $UAS, $LCV, $DM ; } } my @UAS = ("8 0", "9 0", "10 0", "11 0", "12 86400" , "13 0", "14 0", "15 0", "14 0"); my @LCV = ("8 0", "9 0", "10 0", "11 0", "12 0" , "13 0", "14 0", "15 0"); my @DM = ("8 0", "9 0", "10 0", "11 0", "12 0" , "13 0", "14 0", "15 0"); PrintEm \(@UAS, @LCV, @DM); __END__ UAS LCV DM 8 0 8 0 8 0 9 0 9 0 9 0 10 0 10 0 10 0 11 0 11 0 11 0 12 86400 12 0 12 0 13 0 13 0 13 0 14 0 14 0 14 0 15 0 15 0 15 0 14 0 0 0 0 0
Re: Printing format
by Enlil (Parson) on Feb 08, 2003 at 15:50 UTC
    you could format the variables as you concatenate them together using sprintf

    -enlil