Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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
Re^2: Split on every second character
by GrandFather (Cardinal) 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 rifling through the Monastery: (11)
As of 2014-09-22 20:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (201 votes), past polls