如何使用perl为字符串添加n位数

gat*_*ina 0 regex perl

我需要在字符串中添加一定数量的零,以便下面的标量$ newstg出现'1000'.

use strict;
my $number_of_zeros = 3;

my $newstg = '1';
$newstg = $newstg . sprintf("%0$number_of_zerosd\n",$subd); 
print "Subd: $newstg\n";
Run Code Online (Sandbox Code Playgroud)

mob*_*mob 6

使用可以使用/ *支持的表示法在模板中使用变量.sprintfprintf

$newstg .= sprintf("%0*d\n", $number_of_zeros, $subd);
#same as   sprintf("%03d\n", $subd);
Run Code Online (Sandbox Code Playgroud)


sim*_*que 5

您可以使用x重复运算符.

my $number_of_zeros = 3;
print '1' . 0 x $number_of_zeros;
Run Code Online (Sandbox Code Playgroud)