Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: Puzzlement in Splitsville

by Smylers (Pilgrim)
on Jun 20, 2005 at 14:03 UTC ( #468356=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://468356]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2018-08-16 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (167 votes). Check out past polls.

    Notices?