in reply to String character replacement based on position

Is this what you need? You can use substr on the left of the equals.
#!/usr/bin/perl -w use strict; my $str = 'atcgcgtacatcgatac'; substr($str,8,1)= uc (substr($str,8,1)); #substr($str,-9,1)= uc (substr($str,-9,1)); #coincidence that this is +same print $str; #prints atcgcgtaCatcgatac
update: again... I mis-read the direction the first time. Ikegami got it right the first time. The example works out the same whether counting from the left or right. But yes, use negative numbers to count from the right. (-1) is the last character on the right, use positive numbers to count from the left and (0) is the first character from the left, (NOT 1). The "trick" here is using the substr() on the left of the equals.

substr EXPR,OFFSET,LENGTH
If you need more than one character, adjust the LENGTH. BrowserUk's solution is fine also. I suspect the straightforward substr() solution is faster, because the right-hand side substr() does not modify the string, just returns the character specified and uc() is a very fast critter. If in doubt, benchmark.

Replies are listed 'Best First'.
Re^2: String character replacement based on position
by BrowserUk (Patriarch) on Feb 23, 2011 at 05:57 UTC

    For case modifications, tr/// is useful as it operates in place:

    $s = 'atcgcgtacatcgatac';; substr( $s, $_, 1 ) =~ tr[acgt][ACGT] for 3,6,9,12,15;; print $s;; atcGcgTacAtcGatAc

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^2: String character replacement based on position
by ikegami (Patriarch) on Feb 23, 2011 at 06:13 UTC
    That's the 9th character from the right. The 9th character from the right is at index 8.
    substr($str,8,1) = uc(substr($str,8,1));
    or
    $_ = uc for substr($str,8,1);

    Update: Probably should ignore the latter. Harder to read, surely slower and no real benefit.

Re^2: String character replacement based on position
by shu_uemura (Initiate) on Feb 23, 2011 at 06:18 UTC
    Yes! That's exactly what I needed. Thanks so much.