Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PERL substr:使用变量进行替换_Perl - Fatal编程技术网

PERL 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

我正在尝试将一个变量传递给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 ) {
      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”;
}