Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: How can I split a string into chunks of 4 characters

by davido (Cardinal)
on Dec 06, 2013 at 16:54 UTC ( [id://1066012]=note: print w/replies, xml ) Need Help??


in reply to How can I split a string into chunks of 4 characters

my @array = map { substr $string, $_, 4 } 0 .. length( $string ) +- 4;

If the string is really long, use a $position variable and a while loop so that internally the big 0 .. length( $string ) - 4 list doesn't need to be built and stored:

my $pos = 0; while( $pos <= length( $string ) - 4 ) { push @array, substr( $string, $pos++, 4 ); }

Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (2)
As of 2026-04-10 10:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.