I would like to format(truncate/append with chars) a string to a specified length while printing in Perl.
For example
$string='my_string';
printf("%04s",$string);
should print
my_s
also if
$string='my';
I should get
00my
Is there any way to print last four characters ?
ring
and if string is
$string='my';
it should print
00my
You want to do this format string instead of yours:
You need the .4 because this specifies maximum length. (The 4 at the beginning specifies a minimum only)
here are the output of some tests:
Here is the output using the wrong format string. As you can see strings are not truncated.