Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Puzzlement in Splitsville

by Smylers (Pilgrim)
on Jun 20, 2005 at 14:03 UTC ( [id://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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://468356]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-03-28 17:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found