How do I take a string in Perl and split it up into an array with entries two characters long each?
I attempted this:
@array = split(/../, $string);
The pattern passed to split identifies what separates that which you want. If you wanted to use split, you'd use something like
my @pairs = split /(?(?{ pos() % 2 })(?!))/, $string;
or
my @pairs = split /(?=(?:.{2})+\z)/s, $string;
Those are rather poor solutions. Better solutions include:
my @pairs = $string =~ /..?/sg; # Accepts odd-length strings.
my @pairs = $string =~ /../sg;
my @pairs = unpack '(a2)*', $string;