in reply to how to splice an array?

Your array code is assuming that @array contains an array where each element is one letter from the string, which is not what it contains. You can make it contain that by using @array = split( '', "PERLPERLPEBLPEBLPERL" ); or you can just understand that you are dealing with a string, not an array, and take a different approach, such as...

my $string = "PERLPERLPEBLPEBLPERL"; my @answer = ( $string =~ m/(.{4})/g ); print "@answer\n";

We're not surrounded, we're in a target-rich environment!

Replies are listed 'Best First'.
Re^2: how to splice an array?
by bobf (Monsignor) on Oct 12, 2006 at 03:38 UTC

    If it really is a string, substr and unpack could also be used. Here are three ways to do it:

    use strict; use warnings; my $string = "PERLPERLPEBLPEBLPERL"; my $offset = 0; my $step = 4; my @array; # unpack (could modify the pattern to use $step) @array = unpack( '(A4)*', $string ); # substr, original string intact while( $offset < length $string ) { push( @array, substr( $string, $offset, $step ) ); $offset += $step; } # substr, original string destroyed while( length $string ) { push( @array, substr( $string, 0, $step, '' ) ); }