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

Hi all, I have a sequence of letters "ACTGACTGATGCATGCATGCATGCCGTACGTACGTACGT" i want to split this squence by three letters. can any one help me. Thanks

Replies are listed 'Best First'.
Re: split a squence by 3 letters
by ikegami (Patriarch) on May 29, 2009 at 18:47 UTC
    my @groups = $seq =~ /.{3}/sg; # Ignore extra letters my @groups = $seq =~ /.{1,3}/sg; # Return extra letters as a group
Re: split a squence by 3 letters
by almut (Canon) on May 30, 2009 at 00:47 UTC

    Yet another one:

    my $seq = 'ACTGACTGATGCATGCATGCATGCCGTACGTACGTACGT'; my @groups = unpack "(A3)*", $seq;

    (But before you generalize this method, note that pre-5.10.0, unpack's "A" template would unpack bytes, not characters... which would make a difference when unicode is involved. This is irrelevant in this particular case, though.)

Re: split a squence by 3 letters
by MidLifeXis (Monsignor) on May 29, 2009 at 18:50 UTC

    What have you tried? Here is a simple, although unmaintainable, solution that may give you a starting point.

    perl -ne 'chomp; my @data; push @data, $1 while s/^(...)//; print join +(":", @data), ", rest=:$_:\n";'

    --MidLifeXis

    The tomes, scrolls etc are dusty because they reside in a dusty old house, not because they're unused. --hangon in this post

Re: split a squence by 3 letters
by bichonfrise74 (Vicar) on May 29, 2009 at 22:45 UTC
    Try this out...
    #!/usr/bin/perl use strict; my $test = 'ACTGACTGATGCATGCATGCATGCCGTACGTACGTACGT'; for (my $i = 0; $i <= length($test); $i++) { print substr($test, $i, 3) . "\n" if ( ($i % 3) == 0 ); }
Re: split a squence by 3 letters
by Your Mother (Archbishop) on May 30, 2009 at 00:00 UTC

    And one more since it's Friday.

    my @group = grep /\w/, split /(...)/, "ACTGACTGATGCATGCATGCATGCCGTACGTACGTACGT"; print join("+", @group), $/; # ACT+GAC+TGA+TGC+ATG+CAT+GCA+TGC+CGT+ACG+TAC+GTA+CGT

    Do not do it like that. :)