简体   繁体   English

替换数组元素Perl

[英]Replacing array elements Perl

I'm trying to replace an element in my array and my code doesn't seem to work. 我正在尝试替换数组中的元素,但是我的代码似乎无法正常工作。

my @wholeloop = (split //, $loop);

for my $i (0 .. $#wholeloop ) {
    if ( $wholeloop[$i] eq "i" ) {
        $wholeloop[$i] =~ htmlinsert($offset);
        $offset++
    }
}

I've read about problematics of doing stuff while iterating through an array, and maybe is there a better solution. 我已经读过在遍历数组时执行操作的问题,也许有更好的解决方案。 I'm trying to replace specific occurences of a character in a string, and array seemed as a reasonable tool to use. 我正在尝试替换字符串中某个字符的特定出现,而数组似乎是使用的合理工具。

Typically - when iterating on a loop, you don't need to do it via: 通常-在循环中进行迭代时,您无需通过以下方式进行操作:

for ( 0..$#array) {

Because 因为

for ( @array ) { 

will do the same thing, but with an added advantage of $_ being an alias to the array variable. 将执行相同的操作,但是具有$_作为数组变量别名的附加优点。

for my $element ( @wholeloop ) {
    if ( $element eq "i" ) {
       $element = htmlinsert($offset++);
    }
}

$element is an alias so if you change it, you change the array. $element是一个别名,因此如果您更改它,就可以更改数组。 ( $_ will do the same, but I dislike using it when I don't have to, because I think it make less clear code. This is a style/choice matter, rather than a technical one). $_会执行相同的操作,但是我在不需要时不喜欢使用它,因为我认为它的代码不太清晰。这是样式/选择的问题,而不是技术性的问题。)

However for searching and replacing an element in a string - like you're doing - then you're probably better off using one of the other things perl does really well - regular expressions and pattern replacement. 但是,对于搜索和替换字符串中的元素(就像您正在做的那样),则最好使用perl确实做得很好的另一件事-正则表达式和模式替换。 I can't give an example easily though, without knowing what htmlinsert returns. 但是,在不知道htmlinsert返回什么的情况下,我不能轻易给出一个示例。

Something like though: 有点像:

 $loop =~ s/i/newvalue//g;

Will replace all instances of 'i' with a 'new value'. 将用“新值”替换所有“ i”实例。

=~ is Perl's "match regular expression" operator, so unless htmlinsert() returns a regex, it's probably not what you meant to do. =~是Perl的“匹配正则表达式”运算符,因此,除非htmlinsert()返回正则表达式,否则可能不是您想要的。 You probably want to use = . 您可能要使用=

A more Perlish way to do this, though, might be to use the map function. 但是,更Perlish的方法可能是使用map函数。 map takes a block and an array and runs the block with each element of the array in $_ , returning all the values returned by that block. map接受一个块和一个数组,并使用$_中的数组每个元素运行该块,并返回该块返回的所有值。 For example: 例如:

my @wholeloop = map {
    $_ eq "i" ? htmlinsert($offset++) : $_;
} split //, $loop;

(The ? and : perform an "if/else" in a single line; they're borrowed from C. map is borrowed from functional programming languages.) (该?:执行“的if / else”在单行;他们是从C.借用map是从函数式编程语言借来的。)

Perhaps you should use foreach. 也许您应该使用foreach。 It is the most suitable for what you are trying to do here 它最适合您在这里尝试做的事情

my @array;  
foreach ( @array ) {  
    $_ =~ whatever your replacement is;  
}

Now, like Sobrique said, unless htmlinsert returns a RegEx value, that isn't going to work. 现在,就像Sobrique所说的那样,除非htmlinsert返回一个RegEx值,否则它将无法正常工作。 Also, if you could give us context for "$offset", and what its purpose is, that would be really helpful. 另外,如果您可以为我们提供“ $ offset”的上下文及其用途,那将非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM