Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Split on every second character

by linuxer (Deacon)
on Feb 12, 2010 at 23:00 UTC ( #822949=note: print w/ replies, xml ) Need Help??


in reply to Split on every second character

If you are sure that the string is even sized, you can use a simple regex like this:

use strict; use warnings; my $string = "0102030405"; # will miss the last character when string is odd sized my @elements = $string =~ m/(..)/g; print "@elements\n";
Update: modified code


Comment on Re: Split on every second character
Download Code
Replies are listed 'Best First'.
Re^2: Split on every second character
by GrandFather (Sage) on Feb 12, 2010 at 23:05 UTC

    and if you are unsure but want the last character for odd length strings you can:

    my @list = $str =~ /(..?)/g;

    True laziness is hard work
Re^2: Split on every second character
by gri6507 (Deacon) on Feb 12, 2010 at 23:12 UTC
    I didn't think of using the match operator for this. Thanks!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (15)
As of 2015-07-31 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (278 votes), past polls