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

I want to break a string up into chunks of 6 characters, then put all of these into an array. It can be done with substr, chop and reverse, but that's really ugly and inefficient. Can anyone help? The string is purely numerical, if that helps, and I'm on Activeperl 5.8.8. Thanks.

Replies are listed 'Best First'.
Re: Breaking up a string?
by BrowserUk (Patriarch) on Feb 19, 2008 at 15:13 UTC
Re: Breaking up a string?
by Fletch (Bishop) on Feb 19, 2008 at 14:45 UTC

    Maybe if you showed the purported "ugly and inefficient" code someone could help you clean it up . . .

    See also How (Not) To Ask A Question.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Breaking up a string?
by olus (Curate) on Feb 19, 2008 at 15:18 UTC
    use strict; use warnings; my $s="whatever your string looks like"; my @b = $s =~ /.{6}/g; push @b, substr($s, - (6 - int(length($s)/6) ) ); $\="\n"; print foreach @b;
    outputs
    whatev er you r stri ng loo ks lik e
Re: Breaking up a string?
by lidden (Curate) on Feb 19, 2008 at 15:53 UTC
    While you most likely want what BrowserUk said above, here is one that use the fact that your input is numerical.
    use bigint; my $num = 111111555555123456; my @array; while( $num ){ unshift @array, $num % 1_000_000; $num /= 1_000_000; } print "@array\n";
    Though it will be eating leading zeros.
Re: Breaking up a string?
by stiller (Friar) on Feb 19, 2008 at 14:45 UTC
    How inefficient is it? How ugly is your code? How big is your string? What have you tried? Did you try a regex? What did it look like?
      What I tried was using substr to get the first 6 chars, reversing the string, chop'ing it 6 times and reversing it again - all in a repeating loop. There has to be a better way than that :\

        Here's a somewhat more Perlish solution:

        my @sets_of_six = ( $s =~ m{ (.{6}) }xmsg );
        If you got your six characters, why did you perform that reverse - 6 times chop - reverse operation on it? Can't you post a little example code that runs, some example input and the result you get, with an explanation on how your result differ from what you want?
Re: Breaking up a string?
by johngg (Canon) on Feb 19, 2008 at 19:53 UTC
    Given that the OP mentions the substr, chop and reverse methodology I'm wondering if the requirement is to produce an array of arrays of six numbers. Perhaps this is what's required.

    use strict; use warnings; use Data::Dumper; my $string = join q{}, map { int rand 10 } 1 .. 24; my @AoAofSixes = map { [ split m{} ] } $string =~ m{.{6}}g; print Data::Dumper->Dumpxs( [ \ @AoAofSixes ], [ q{*AoAofSixes} ] );

    The output.

    @AoAofSixes = ( [ '1', '9', '6', '8', '4', '7' ], [ '1', '5', '1', '9', '8', '5' ], [ '9', '6', '6', '0', '1', '1' ], [ '3', '8', '3', '2', '5', '8' ] );

    It's possible I'm barking up the wrong tree but it would explain the why of the "really ugly and inefficient" code.

    Cheers,

    JohnGG

    Update: Fixed typo