You can build the format string:

my $myVar = 'ABCD'; #(Left Aligned, padded with spaces)
#$newVar = sprintf("%-10s,$myVar);
#$newVar should have 'ABCD      '; # Works

while ( 1 ) {
   printf "<L>eft or <R>ight: ";
   chomp(my $MyInput = <STDIN>);
   last if ( $MyInput =~ /^ex/i );
   my $MySign = '-';
   $MySign = '' if ( $MyInput =~ /^r/i );
   my $MyFormat = '%' . $MySign . '10s';
   printf "${MyFormat}\n", $myVar;
 }

Depending on whether left or right, it will print out as such. Could do this
with sprintf also.

Wags ;)

-----Original Message-----
From: Shishir K. Singh [mailto:[EMAIL PROTECTED]]
Sent: Thursday, June 20, 2002 22:42
To: [EMAIL PROTECTED]
Subject: Formatting


Hi, 

I need to format a string in a fixed width field. The string may be less
than the length of the format, or may be greater. If less, then it should
get  padded with spaces (left or right justified , like using - in sprintf),
if greater, then the string should get truncated to the exact length. 


eg $myVar = 'ABCDEFGHIJKLMNOP';
$newVar = sprintf("%10s,$myVar);
$newVar should have 'ABCDEFGHIJ'; # But has the full length i.e.
'ABCDEFGHIJKLMNOP'

eg $myVar = 'ABCD'; (Right Aligned, padded with spaces)
$newVar = sprintf("%10s,$myVar);
$newVar should have '      ABCD'; # Works

eg $myVar = 'ABCD'; (Left Aligned, padded with spaces)
$newVar = sprintf("%-10s,$myVar);
$newVar should have 'ABCD      '; # Works


I am not able to lay my finger on the correct format to achieve 1st and the
2nd with the same format. Am I missing something , or is there another way
out? Any help would be greatly appreciated.

Thanks
Shishir

 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to