in reply to Puzzlement in Splitsville

It is possible to use split for this, but the only solution I know of requires a filtering through grep:

@codons = grep $_, split /(.{3})/, 'atgactaatagcagtgg';

As demerphq pointed out, unpack is a better way to achieve this. But if you're going to use split, note that that grep condition is wrong. In particular if the last character is a zero and it should be in an an element of its own then it will get omitted:

my @codons = grep $_, split /(.{3})/, 'atgactaatagcagt0';

Explicitly testing for the empty string is required:

my @codons = grep { $_ ne '' } split /(.{3})/, 'atgactaatagcagt0';

Smylers

Replies are listed 'Best First'.
Re^2: Puzzlement in Splitsville
by demerphq (Chancellor) on Jun 21, 2005 at 11:05 UTC

    In particular if the last character is a zero and it should be in an an element of its own then it will get omitted:

    if you say grep length($_), ... you avoid this problem...

    ---
    $world=~s/war/peace/g