tkguifan has asked for the wisdom of the Perl Monks concerning the following question:

# input my $input='rnbqkbnrpppppppp PPPPPPPPRNB +QKBNR'; # desired output: my @array; $array[0]='rnbqkbnr'; $array[1]='pppppppp'; $array[2]=' '; $array[3]=' '; $array[4]=' '; $array[5]=' '; $array[6]='PPPPPPPP'; $array[7]='RNBQKBNR';
  • Comment on How can one decompose a string into an array of 8 character blocks?
  • Download Code

Replies are listed 'Best First'.
Re: How can one decompose a string into an array of 8 character blocks?
by ikegami (Patriarch) on Feb 03, 2015 at 14:03 UTC
    unpack '(a8)*', $str # Supports incomplete blocks $str =~ /.{8}/sg $str =~ /.{1,8}/sg # Supports incomplete blocks
Re: How can one decompose a string into an array of 8 character blocks?
by choroba (Cardinal) on Feb 03, 2015 at 14:05 UTC
    TIMTOWTDI:
    my @array = grep length, split /(.{8})/, $input;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Can't test, but

       my @array =  $input =~ /(.{8})/g;

      looks easier...

      Cheers Rolf

      PS: Je suis Charlie!

        You are right. You can even further simplify it to
        my @array = $input =~ /.{8}/g;
        لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: How can one decompose a string into an array of 8 character blocks?
by Discipulus (Canon) on Feb 04, 2015 at 10:13 UTC
    TIMToady using core module Text::Wrap:
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; use Text::Wrap qw(wrap $columns $huge); $columns = 8; # Wrap at 8 characters my @array; while (<DATA>){push @array, wrap('', '', $_);} print Dumper (\@array) __DATA__ rnbqkbnrpppppppp PPPPPPPPRNBQKBNR

    HtH
    L*
    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.