http://www.perlmonks.org?node_id=1000656

viktor has asked for the wisdom of the Perl Monks concerning the following question:

Hi All,

I am new perl programmer may be its easy but I would like to know how I can extract the string after removing the substring e.g

$string="ATATTTATATTAT"; $removed=substr($string,0,3)

Now the $removed contains ATA but I want to extract the rest so that now my $string should contain TTTATATTAT.

Replies are listed 'Best First'.
Re: Extract string after removing the substring
by toolic (Bishop) on Oct 24, 2012 at 15:28 UTC
    Here's one way:
    use warnings; use strict; my $string = "ATATTTATATTAT"; my $removed = substr($string, 0, 3); $string = substr($string, 3); print "$string $removed\n"; __END__ TTTATATTAT ATA

    UPDATE: Fixed bug. Thanks AnomalousMonk

Re: Extract string after removing the substring
by Athanasius (Archbishop) on Oct 24, 2012 at 15:31 UTC

    Here’s another way:

    If you don’t need the original string anymore, just give an empty string as the REPLACEMENT argument to substr:

    1:26 >perl -E " $string = qq[ATATTTATATTAT]; say $string; $removed = +substr($string, 0, 3, q[]); say $removed; say $string; " ATATTTATATTAT ATA TTTATATTAT 1:30 >

    Hope that helps,

    Athanasius <°(((><contra mundum

      thanks!!
Re: Extract string after removing the substring
by Kenosis (Priest) on Oct 24, 2012 at 17:29 UTC

    Just in case this may be helpful as a future reference, here are two non-substr ways to get the results you want:

    use Modern::Perl; my $string = 'ATATTTATATTAT'; my ( $subStr1, $theRest1 ) = unpack '(a3)(a*)', $string; say $subStr1; say $theRest1, "\n"; my ( $subStr2, $theRest2 ) = $string =~ /(.{3})(.+)/; say $subStr2; say $theRest2;

    Output:

    ATA TTTATATTAT ATA TTTATATTAT

    unpack expands the original string into two chunks: the first being three characters and the second is the rest of the string. The second method uses two captures within a regex, matching three characters and then the remaining characters.

      ... two non-substr ...

      ... and non-destructive to the original string, as opposed to the 4-argument substr approach.

        Excellent point, AnomalousMonk!

Re: Extract string after removing the substring
by kcott (Archbishop) on Oct 25, 2012 at 10:45 UTC

    G'day viktor,

    I initially came up with an alternative solution using regex captures:

    $ perl -Mstrict -Mwarnings -E ' my ($string, $offset, $length) = qw{ATATTTATATTAT 0 3}; $string =~ /^(.{$offset})(.{$length})(.*)$/; say "Extracted: ", $2 // ""; say "Remainder: ", ($1 // "") . ($3 // ""); ' Extracted: ATA Remainder: TTTATATTAT

    I then considered that this could be made into a function that was called like substr (i.e. substr EXPR, OFFSET, LENGTH). This originally looked something like this:

    sub split_string ($$$) { my ($string, $offset, $length) = @_; $string =~ /^(.{$offset})(.{$length})(.*)$/; return (($1 // ''), ($2 // ''), ($3 // '')); }

    This can be called as split_string EXPR, OFFSET, LENGTH and returns a three-element array consisting of: whatever was on the left of the extracted string; the extracted string itself; and, whatever was on the right of the extracted string. Simple usage would be something like:

    my ($left, $extract, $right) = split_string $string, $offset, $length; my $remainder = $left . $right;

    Of course, substr also allows a negative OFFSET, a negative LENGTH and the omission of LENGTH altogether. While I realise this is possibly approaching overkill for your requirements, I tinkered with the code to add this functionality. The code, test data and output is quite lengthy: click on the Read More link to view. [Note: unlike substr, split_string does not take a REPLACEMENT argument nor can it be used as an lvalue.]

    -- Ken