加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Perl:你如何将其转换为子程序?

发布时间:2020-12-16 06:16:38 所属栏目:大数据 来源:网络整理
导读:子程序的目的是检查传入的其他标量中是否包含单词pasttense,然后执行替换并返回该替换. 原始工作代码: if ($sentences[$i] =~ /b$pasttense/i and $firstword[1] =~ /b$pasttense/i) { $subsentences[$i] =~ s/$pasttense/ **$pasttense** /ig; } elsif (
子程序的目的是检查传入的其他标量中是否包含单词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$pluralsuffixb/i) {
            $subsentences[$i] =~ s/$search_key$pluralsuffix/ **$search_key$pluralsuffix** /ig;
                        }
        elsif ($sentences[$i] =~ /b$search_key/i and $firstword[1] =~ /b$search_keyb/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,1); 
my $pastpart = changeVerbForm($presentword,2);
my $thirdsing = changeVerbForm($presentword,3);
my $presentpart = changeVerbForm($presentword,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$pluralsuffixb/i) {
        return s/$presentword$pluralsuffix/ **$presentword$pluralsuffix** /ig;
    }
    elsif ($check2 =~ /b$presentword/i and $check1 =~ /b$presentwordb/i) {
        return s/b$presentword/ **$presentword**/gi;
    }
}

它被称为:

$subsentences[$i] =~ suffix_changer_and_highlighter($search_key,$firstword[1],$sentences[$i]);

我得到一个错误,说s ///未初始化.对不起,如果这是基本的,但我是Perl的新手.如果您需要更多信息,请告诉我.

非常感谢.

解决方法

看起来您的所有测试和替换都遵循相同的模式.所以我将if / elsif / logic链转换为数据,并处理一系列可能的测试.

use strict;
use warnings;

use My::VerbTenseChanger qw( changeVerbForm );

sub suffix_changer_and_highlighter {

    my ($presentword,$check2) = @_; ##search_key##parsewords[1] or firstword[1]
                                              ##sentences
    my $pluralsuffix = 's';                   ##unless ends in y

    my @verbforms = map changeVerbForm( $presentword,$_ ),1..4;

    for my $form ( @verbforms ) {
         if( $check1 =~ /b$form/i and $check2 =~ /b$form/i ) {
             $check2 =~ s/b$form/ **$form**/ig;
             return $check2;
         }
    }

    return;
}

另外,不要使用require在主脚本中执行perl库.自从Perl 4以来,这已经不是标准做法 – 也就是90年代初期.在屁股上踢你的教程.它们已经过时了.

而是创建一个模块并导出您的函数,以便可以导入它们.

package My::VerbTenseChanger;  # Declare a new namespace.

use strict;
use warnings;

use Exporter qw( import );  # Import the import function from exporter.

our @EXPORT_OK = qw( changeVerbForm );  # List the functions that can be exported.

sub changeVerbForm {

    # Here's your normal code you had before.
}   


1;   # Make sure you end the file with a TRUE value like 1.

use将期望上面的包相对于模块搜索路径中的任何位置(@INC)位于My / VerbTenseChanger.pm文件中.保存项目内容的最简单位置与主程序位于同一目录中.

MyGrammarianProject
 |- verby_magic <------------- This is your script.
 - My <---------------------- This is a subdirectory
    - VerbTenseChanger.pm  <- This is the module file

哇,最后,你可能想看看各种Lingua modules on CPAN.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读