Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I insert a lot of whitespace in Perl?

I need to buff out a line of text with a varying but large number of whitespace. I can figure out a janky way of doing a loop and adding whitespace to $foo, then splicing that into the text, but it is not an elegant solution.

like image 649
Timo Avatar asked Sep 30 '09 11:09

Timo


3 Answers

I need a little more info. Are you just appending to some text or do you need to insert it?

Either way, one easy way to get repetition is perl's 'x' operator, eg.

" " x 20000

will give you 20K spaces.

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

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

BTW, Perl has an extensive set of operators - well worth studying if you're serious about the language.

UPDATE: The question as originally asked (it has since been edited) asked about 20K spaces, not a "lot of whitespace", hence the 20K in my answer.

like image 50
dave Avatar answered Sep 24 '22 21:09

dave


If you always want the string to be a certain length you can use sprintf:

For example, to pad out $var with white space so it 20,000 characters long use:

$var = sprintf("%-20000s",$var);
like image 20
Dave Webb Avatar answered Sep 22 '22 21:09

Dave Webb


use the 'x' operator:

print ' ' x 20000;
like image 26
dalloliogm Avatar answered Sep 21 '22 21:09

dalloliogm