How can I split a string into chunks of two characters each in Perl?

53,003

Solution 1

@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.

Solution 2

If you really must use split, you can do a :

grep {length > 0} split(/(..)/, $string);

But I think the fastest way would be with unpack :

unpack("(A2)*", $string);

Both these methods have the "advantage" that if the string has an odd number of characters, it will output the last one on it's own.

Solution 3

Actually, to catch the odd character, you want to make the second character optional:

@array = ( $string =~ m/..?/g );

Solution 4

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;
Share:
53,003
Jax
Author by

Jax

Updated on April 01, 2020

Comments

  • Jax
    Jax about 4 years

    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);
    

    but did not get the expected results.

    Ultimately I want to turn something like this

    F53CBBA476
    

    in to an array containing

    F5 3C BB A4 76
    
  • mat
    mat over 15 years
    yup, but it's way slower than unpack.
  • Chas. Owens
    Chas. Owens about 15 years
    Since it looks like he is working with hex characters, so this is a bit of a moot point, but A only works for ASCII characters. The split should work for any encoding, but you might want to add a /s to the regex so "\n" will be matched by ".".
  • ikegami
    ikegami almost 13 years
    Should be a2, not A2. The former strips trailing whitespace, which is at best superfluous.
  • Bhagyesh Dudhediya
    Bhagyesh Dudhediya about 6 years
    Hi @ikegami, Is there a way I can divide this string into 4 parts such that 1st 2 parts has 2 chars each and last 2 parts have 3 chars each.. i.e F5 3C BBA 476