简体   繁体   中英

Perl replace every occurrence differently

In a perl script, I need to replace several strings. At the moment, I use:

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

The aim is to format in a FASTA file every sequence name. 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.)

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:

$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. I use an anonymous code ref so that I can return at any point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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