简体   繁体   English

Perl用不同的方式替换每种情况

[英]Perl replace every occurrence differently

In a perl script, I need to replace several strings. 在perl脚本中,我需要替换几个字符串。 At the moment, I use: 此刻,我使用:

$fasta =~ s/\>[^_]+_([^\/]+)[^\n]+/\>$1/g;

The aim is to format in a FASTA file every sequence name. 目的是在每个序列名称中格式化FASTA文件。 It works well in my case so I don't need to touch this part. 就我而言,它运作良好,因此我不需要接触这部分。 However, it happens that a sequence name appears several times in the file. 但是,碰巧序列名称在文件中出现了几次。 I must not have at the end twice - or more - the same sequence name. 最后一定不要重复两次(或多次)相同的序列名。 I thus need to have for instance: 因此,我需要例如:

seqName1
seqName2
etc.

(instead of seqName, seqName, etc.) (而不是seqName,seqName等)

Is this possible to somehow process differently every occurrence automatically? 是否有可能以某种方式自动处理每个事件? I don't know how many sequence there are, if there are similar names, etc. An idea would be to concatenate a random string at every occurrence for instance, hence my question. 我不知道有多少序列,是否有相似的名称,等等。例如,一个想法是在每次出现时连接一个随机字符串,因此,我的问题是。

Many thanks. 非常感谢。


John perfectly solved it and chepner helped with the smart idea to avoid conflicts, here is the final result: John完美地解决了这个问题,chepner帮助这个聪明的主意避免了冲突,这是最终结果:

$fasta =~ s/\>[^_]+_([^\/]+)[^\n]+/
    sub {
        return '>'.$1.$i++;
    }->();
/eg;

Many many thanks. 非常感谢。

I was actually trying to do something like this the other day, here's what I came up with 前几天我实际上是在尝试做这样的事情,这就是我的想法

$fasta =~ s/\>[^_]+_([^\/]+)[^\n]+/

    sub {

        # return random string

    }->();

/eg;

the \\e modifier interprets the substitution as code, not text. \\e修饰符将替换解释为代码,而不是文本。 I use an anonymous code ref so that I can return at any point. 我使用匿名代码ref,以便可以随时返回。

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

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