in reply to Re: how to splice an array?
in thread how to splice an array?
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, '' ) ); }
|
|---|