in reply to Possible to use variable in printf statement?


GotToBTru,

Thanks for the response. Yes the first case I did read about that. But whenever I tried using it my script would execute and then never stop... Weird, would not stop printing blank lines in my terminal? Don't know why. But you second case is interesting I'll have to take a look into that. Thanks again!


TomDLux,

Gotcha, thanks for the reply. Makes sense...



Thanks to everyone who responded.
Here is what I ended up doing:

my @colLength; sub calcMaxColumn { # $i will hold index locations of column lengths in the array @co +lLengths. my $i; # Loop through the array @AoA. And if the length of the current c +olumn is greater then the length of the # same column in the previous row then set the greater number equ +al to that location in @colLength. for (my $x = 0; $x <= $#AoA; $x++) { $i = 0; for (my $y = 0; $y <= 11; $y++) { if ($colLengths[$i] < length($AoA[$x][$y])) { $colLengths[$i] = length($AoA[$x][$y]); } $i++; } } }


So basically I created the sub routine above that calculates the longest length of the string in each column. Then I used what you guys showed me, to use a variable (in my case $colLength[]) to format the output.


And here is how I printed:
my $strLen = 0; for (my $x = 0; $x <= $#AoA; $x++) { #If the current line is an owner then there will only be 10 el +ements. if ($AoA[$x][0] eq 'OWNER') { $waitline = 10; } #ELSE the line is a user waiting and we should add the time wa +iting to the end of that line, 11 total elements. else { $waitline = 11; } for (my $y = 0; $y <= $waitline; $y++) { $strLen = $colLengths[$y]; printf OUTDATA "%-${strLen}s ", $AoA[$x][$y]; } print OUTDATA "\n"; }


It works great!!! Thanks again guys. your the bestest...

Thanks,
Matt