On Thu, 2002-06-20 at 22:42, Shishir K. Singh wrote:
> Hi, 
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.
> 
you can try using pack:

$myVar = 'ABCDEFGHIJKLMNOP';
$newVar = pack 'A10', $myVar;
$newVar eq 'ABCDEFGHIJ';

$myVar = 'ABCD';
$newVar = pack 'A10', $myVar;
$newVar eq 'ABCD      ';



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

Reply via email to