PERL substr:使用变量进行替换
我正在尝试将一个变量传递给substr 因此,每次“工作”都将被替换 以递增的数字PERL substr:使用变量进行替换,perl,Perl,我正在尝试将一个变量传递给substr 因此,每次“工作”都将被替换 以递增的数字 #!/usr/bin/perl -w use strict; my $find = "work"; my $string = "why doesnt this work?"; my $idx; for(my $replace = 0; $replace < 3; $replace++) { if( ($idx= index($string, $find)) > -1 ) { s
#!/usr/bin/perl -w
use strict;
my $find = "work";
my $string = "why doesnt this work?";
my $idx;
for(my $replace = 0; $replace < 3; $replace++) {
if( ($idx= index($string, $find)) > -1 ) {
substr($string, $idx, 4, $replace);
}
print "[#$replace] $string\n";
}
如何在substr中使用变量 在
$string
上首次调用substr()
后,此字符串中没有“工作”,请尝试以下操作:
my $find = "work";
my $org_string = "why doesnt this work?";
my $idx;
for(my $replace = 0; $replace < 3; $replace++) {
my $string = $org_string;
if( ($idx= index($string, $find)) > -1 ) {
substr($string, $idx, 4, $replace);
}
print "[#$replace] $string\n";
}
my$find=“work”;
my$org\u string=“为什么这不起作用?”;
我的$idx;
对于(我的$replace=0;$replace<3;$replace++){
my$string=$org\u字符串;
if(($idx=index($string,$find))>-1){
substr($string,$idx,4,$replace);
}
打印“[#$replace]$string\n”;
}
在对$string
首次调用substr()
后,此字符串中没有“工作”,请尝试以下操作:
my $find = "work";
my $org_string = "why doesnt this work?";
my $idx;
for(my $replace = 0; $replace < 3; $replace++) {
my $string = $org_string;
if( ($idx= index($string, $find)) > -1 ) {
substr($string, $idx, 4, $replace);
}
print "[#$replace] $string\n";
}
my$find=“work”;
my$org\u string=“为什么这不起作用?”;
我的$idx;
对于(我的$replace=0;$replace<3;$replace++){
my$string=$org\u字符串;
if(($idx=index($string,$find))>-1){
substr($string,$idx,4,$replace);
}
打印“[#$replace]$string\n”;
}