如何在 Perl 中用新字符串替换最后一个字符串字符
How to replace the last string character with a new string in Perl
我正在 B 列中迭代 excel sheet。如果单元格包含 "
,则将其替换为 Inches
。但是,当使用 substr 函数时,$newval
仍为空白。在测试 运行 中,$cell
和 $ind
都包含正确的值。我没有收到任何错误和警告。
为什么$newval
是空白的?我在网上找到了这个 substr-example,但似乎无法弄清楚我在做什么:
An alternative to using substr as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice.
my $s = "The black cat climbed the green tree";
my $z = substr $s, 14, 7, "jumped from"; # climbed
# $s is now "The black cat jumped from the green tree"
这是我的代码:
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
my $cell = $sheet->Range("B". $i)->{Value};
print $cell . "\n"; # Notebook 24"
my $ind = index($cell, '\"');
print $ind ."\n"; # -1
my $newval = substr($cell, $ind, 0, " Inches");
print $newval . "\n"; # (blank)
<STDIN>;
$sheet->Range("B". $i)->{Value} = $newval;
}
...
编辑:(在控制台中添加了示例输出)
编辑 2:(指数现在为正且正确)
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
my $cell = $sheet->Range("B". $i)->{Value};
print $cell . "\n"; # Notebook 24"
my $ind = index($cell, '"');
print $ind ."\n"; # 11
my $newval = substr($cell, $ind, 0, " Inches");
print $newval . "\n"; # (blank)
<STDIN>;
$sheet->Range("B". $i)->{Value} = $newval;
}
...
控制台输出:
双引号在单引号中不需要反斜杠。
index($cell, '\"');
相反:它搜索后跟双引号的反斜杠,它不存在于字符串中,因此返回值为 -1。
say index 'Notebook 24"', '\"'; # -1
say index 'Notebook 24"', '"'; # 11
解决方法:
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
$sheet->Range("B". $i)->{Value} =~ s/"/ Inches/;
}
...
我正在 B 列中迭代 excel sheet。如果单元格包含 "
,则将其替换为 Inches
。但是,当使用 substr 函数时,$newval
仍为空白。在测试 运行 中,$cell
和 $ind
都包含正确的值。我没有收到任何错误和警告。
为什么$newval
是空白的?我在网上找到了这个 substr-example,但似乎无法弄清楚我在做什么:
An alternative to using substr as an lvalue is to specify the replacement string as the 4th argument. This allows you to replace parts of the EXPR and return what was there before in one operation, just as you can with splice.
my $s = "The black cat climbed the green tree";
my $z = substr $s, 14, 7, "jumped from"; # climbed
# $s is now "The black cat jumped from the green tree"
这是我的代码:
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
my $cell = $sheet->Range("B". $i)->{Value};
print $cell . "\n"; # Notebook 24"
my $ind = index($cell, '\"');
print $ind ."\n"; # -1
my $newval = substr($cell, $ind, 0, " Inches");
print $newval . "\n"; # (blank)
<STDIN>;
$sheet->Range("B". $i)->{Value} = $newval;
}
...
编辑:(在控制台中添加了示例输出)
编辑 2:(指数现在为正且正确)
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
my $cell = $sheet->Range("B". $i)->{Value};
print $cell . "\n"; # Notebook 24"
my $ind = index($cell, '"');
print $ind ."\n"; # 11
my $newval = substr($cell, $ind, 0, " Inches");
print $newval . "\n"; # (blank)
<STDIN>;
$sheet->Range("B". $i)->{Value} = $newval;
}
...
控制台输出:
双引号在单引号中不需要反斜杠。
index($cell, '\"');
相反:它搜索后跟双引号的反斜杠,它不存在于字符串中,因此返回值为 -1。
say index 'Notebook 24"', '\"'; # -1
say index 'Notebook 24"', '"'; # 11
解决方法:
...
if($sheet->Range("B". $i)->{Value} =~ m/\"/)
{
$sheet->Range("B". $i)->{Value} =~ s/"/ Inches/;
}
...