代码之家  ›  专栏  ›  技术社区  ›  tavalendo

基于位置替换字符串中的字符

  •  1
  • tavalendo  · 技术社区  · 7 年前

    以下是我所做的:

    my ($pos, $rep) = @ARGV;
    
    print ("Give me the string: ");
    chomp(my $string = <STDIN>);
    
    print ("The modified string is ", substr($seq, $pos, 1, $rep),"\n");
    

    在终端运行时:

    perl myprogram.pl 4 B
    Give me the string: eeeeee
    The modified string is e
    

    我想要的输出是:

    有什么不对劲的线索吗?

    1 回复  |  直到 7 年前
        1
  •  4
  •   melpomene    7 年前

    引用 perldoc -f substr :

    使用的替代方法 substr 作为左值,将替换字符串指定为第4个参数。这允许您替换EXPR和 把以前的东西还给我 splice .

    换句话说, 子字符串

    substr $seq, $pos, 1, $rep;
    # or alternatively:
    #  substr($seq, $pos, 1) = $rep;
    print "The modified string is $seq\n";