【发布时间】:2011-06-01 03:50:17
【问题描述】:
该子例程的目的是检查单词 Pasttense 是否在传入的其他两个标量中,然后执行替换并返回。
原始工作代码:
if ($sentences[$i] =~ /\b$pasttense/i and $firstword[1] =~ /\b$pasttense/i) {
$subsentences[$i] =~ s/$pasttense/ **$pasttense** /ig;
}
elsif ($sentences[$i] =~ /\b$pastpart/i and $firstword[1] =~ /\b$pastpart/i) {
$subsentences[$i] =~ s/$pastpart/ **$pastpart** /ig;
}
elsif ($sentences[$i] =~ /\b$thirdsing/i and $firstword[1] =~ /\b$thirdsing/i) {
$subsentences[$i] =~ s/$thirdsing/ **$thirdsing** /ig;
}
elsif ($sentences[$i] =~ /\b$presentpart/i and $firstword[1] =~ /\b$presentpart/i) {
$subsentences[$i] =~ s/$presentpart/ **$presentpart** /ig;
}
elsif ($sentences[$i] =~ /\b$search_key$pluralsuffix/i and $firstword[1] =~ /$search_key$pluralsuffix\b/i) {
$subsentences[$i] =~ s/$search_key$pluralsuffix/ **$search_key$pluralsuffix** /ig;
}
elsif ($sentences[$i] =~ /\b$search_key/i and $firstword[1] =~ /\b$search_key\b/i) {
$subsentences[$i] =~ s/\b$search_key/ **$search_key**/gi;
}
我的尝试:
sub suffix_changer_and_highlighter {
my ($presentword, $check1, $check2) = @_; ##search_key##parsewords[1] or firstword[1]##sentences
my $pluralsuffix = 's'; ##unless ends in y
require 'verbTenseChanger.pl';
my $pasttense = changeVerbForm($presentword,0,1);
my $pastpart = changeVerbForm($presentword,0,2);
my $thirdsing = changeVerbForm($presentword,0,3);
my $presentpart = changeVerbForm($presentword,0,4);
if ($check2 =~ /\b$pasttense/i and $check1 =~ /\b$pasttense/i) {
return s/$pasttense/ **$pasttense** /ig;
}
elsif ($check2 =~ /\b$pastpart/i and $check1 =~ /\b$pastpart/i) {
return s/$pastpart/ **$pastpart** /ig;
}
elsif ($check2 =~ /\b$thirdsing/i and $check1 =~ /\b$thirdsing/i) {
return s/$thirdsing/ **$thirdsing** /ig;
}
elsif ($check2 =~ /\b$presentpart/i and $check1 =~ /\b$presentpart/i) {
return s/$presentpart/ **$presentpart** /ig;
}
elsif ($check2 =~ /\b$presentword$pluralsuffix/i and $check1 =~ /$presentword$pluralsuffix\b/i) {
return s/$presentword$pluralsuffix/ **$presentword$pluralsuffix** /ig;
}
elsif ($check2 =~ /\b$presentword/i and $check1 =~ /\b$presentword\b/i) {
return s/\b$presentword/ **$presentword**/gi;
}
}
它的调用者:
$subsentences[$i] =~ suffix_changer_and_highlighter($search_key, $firstword[1], $sentences[$i]);
我收到一条错误消息,提示 s/// 未初始化。对不起,如果这是基本的,但是我是 Perl 的新手。如果您需要更多信息,请告诉我。
非常感谢。
【问题讨论】:
-
如果 perl >=5.010 试试
given-whensee perldoc perlsyn
标签: perl