Perl数组和字符串作为方法参数

病毒

我有一个提供两个参数的方法:一个数组和一个字符串问题是,当我初始化两个不同的变量时,一个用于数组,一个用于字符串,我在第一个变量中得到数组的第一个元素,在第二个变量中得到第二个元素。我的问题是如何将整个数组存储在一个变量中,将字符串存储在另一个变量中?

Analyze.pm

sub analyze {
    my $self = shift;
    my ($content, $stringToSearch) = @_; 
    # my ($stringToSearch) = @_;
    print "$stringToSearch";
    if (!defined($stringToSearch) or length($stringToSearch) == 0) { die 'stringToSearch is not defined yet! ' }
    foreach my $element ($content) {
        #print "$element";
        my $loc = index($element, $stringToSearch);
        # print "$loc\n";
        given ($stringToSearch) {
            when ($stringToSearch eq "Hi") {
                if ($loc != 0) {
                    print "Searched word is Hi \n"; 
                } else {
                    print "No word found like this one! "
                }
            }
            #when ($stringToSearch == 'ORIENTED_EDGE') {
            #   print 'Searched word is ORIENTED_EDGE';
            #} # Printed out because i dont need it now!
        }
        break; # For testing
    }
}

example.pm

my @fileContent = ('Hi', 'There', 'Its', 'Me')
my $analyzer = Analyze->new();
$analyzer->analyze(@fileContent, 'Hi');

当我更改$content@content它时,将数组和字符串的所有值放入@content

我希望有人能够帮助我。我是Perl的初学者。提前致谢

池上

您不能将数组传递给子(或方法),而只能传递标量列表。因此,

$analyzer->analyze(@lines, 'Hi');

与...相同

$analyzer->analyze($lines[0], $lines[1], ..., 'Hi');

这意味着以下内容将无法正常工作:

my (@strings, $target) = @_;

Perl不知道原始数组中有多少个项目,因此将它们全部放入中@strings,而$target未定义。


解决方案

您可以执行以下操作:

sub analyze {
    my $self = shift;
    my $target = pop;
    my @strings = @_
    for my $string (@strings) {
       ...
    }
}

$analyzer->analyze(@lines, 'Hi')

还是没有不必要的副本:

sub analyze {
    my $self = shift;
    my $target = pop;
    for my $string (@_) {
       ...
    }
}

$analyzer->analyze(@lines, 'Hi')

先通过目标会更容易。

sub analyze {
    my ($self, $target, @strings) = @_;
    for my $string (@strings) {
       ...
    }
}

$analyzer->analyze('Hi', @lines)

还是没有不必要的副本:

sub analyze {
    my $self = shift;
    my $target = shift;
    for my $string (@_) {
       ...
    }
}

$analyzer->analyze('Hi', @lines)

您还可以将引用传递给数组(以您喜欢的任何顺序),因为引用是标量。

sub analyze {
    my ($self, $target, $strings) = @_;
    for my $string (@$strings) {
       ...
    }
}

$analyzer->analyze('Hi', \@lines)

我会倒数第二。它遵循与众所周知的相同的一般模式grep

my @matches = grep { condition($_) } @strings;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

数组和字符串作为 PHP 中的 Soap 参数

字符串文字作为方法的参数

返回以整数和字符串为参数的数组字符串的方法

如何从参数作为数组创建“公共字符串数组”?

C 编程字符或字符串数组作为函数参数

使用JAXRPC将字符串数组作为参数发送到Web服务方法

将带有字符串数组作为参数的glib C方法转换为Go

在bash中将字符串或数组作为参数传递

在函数中将字符串数组作为输入参数传递

用参数替换函数作为字符串数组

使用字符串数组作为函数参数

在 Perl 中传递多个数组,字符串参数

合并字符串和变量作为参数?

VBA使用字符串数组作为子字符串参数InStr函数(Excel)

变量和字符串作为数组键

类并将字符串作为方法的参数传递

在方法重载中将字符串作为参数传递

“包括?” 对于作为数组元素和作为字符串传递的相同字符串,方法的工作方式有所不同

'replace'字符串方法如何与以脱字符号(正则表达式)和字符串函数作为参数一起使用?

可以将数字或数字数组作为参数并返回字符串或字符串数组的函数

从动态参数创建字符串数组和字符串变量

参数类型不兼容 - 字符串和字符串数组

当期望的返回类型和返回值作为字符串参数传递时,比较方法的返回值

传递空字符串作为参数

Thymeleaf 使用参数作为字符串

传递替换字符串作为参数

传递字符串作为参数

字符串作为Java中的参数

如何将二维数组类型字符(字符串)作为函数参数传递?