A user recently asked:
How do I take a string in Perl and split it up into an array with entries two characters long each?
Ultimately I want to turn something like this
F53CBBA476
into and array containing
F5 3C BB A4 76
This was my answer:
@array = ( $string =~ m/../g );
The pattern-matching operator behaves in a special way in a list context in Perl. It processes the operation iteratively, matching the pattern against the remainder of the text after the previous match. Then the list is formed from all the text that matched during each application of the pattern-matching.
I’m posting to my blog the questions I’ve answered on StackOverflow, which earned the “Good Answer” badge. This was my answer to “How can I split a string into chunks of two characters each in Perl?“
Leave a Reply