How to insert many spaces in Perl?

I need to align a line of text with a variable, but a lot of spaces. I can find a janky way to loop and add a space in $ foo and then splic it into the text, but this is not an elegant solution.

+5
source share
3 answers

I need a little more information. Are you just adding any text or do you need to insert it?

In any case, one easy way to get the repetition is with the perl 'x' operator, for example.

" " x 20000

will give you 20 thousand spaces.

If you have an existing string ($ s say) and want to put it at 20K, try

$s .= (" " x (20000 - length($s)))

BTW, Perl - , .

: , ( ), 20K , " ", 20K .

+29

, , sprintf:

, $var , 20 000 :

$var = sprintf("%-20000s",$var);
+10

use the 'x' operator:

print ' ' x 20000;
+4
source

All Articles