Splitting a String in Perl

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


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?







One response to “Splitting a String in Perl”

  1. Wolf Avatar

    Perl is the best scripting language for Text processing and handle regex. I have posted few articles related to those at my blog


    Also Perl’s Cpan has lots of support that I don’t even need to think extra while developing project. I didn’t find such help on other programming language except Java and .NET

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.