http://www.perlmonks.org?node_id=822949


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

Replies are listed 'Best First'.
Re^2: Split on every second character
by GrandFather (Saint) 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!