Nyimi Jose wrote:
>
> I have a string which i know, it is a concatenation of "fixed
> length of substring". I would like to split it based on this
> known length and get as result An array containing each
> substring.
>
> Let say the known length is 2. If the string is 'abcd', my array
> should be ('ab', 'dc') And if my string is 'abcdef', my array
> should be ('ab', 'dc', 'ef').
>
> I was thinking about unpack function but how to make it dynamic?
>
> I mean writing :
> my @items = unpack('A2 A2', $str);
> Will work for $str='abdc';
> Not for $str='abcdef';
>
> Any ideas ?

Hi Nyimi.

The subroutine 'split_len' below will do what you want.

HTH,

Rob


use strict;
use warnings;

my $string = join '', 'A' .. 'Z';
print map "$_\n", split_len(7, $string);

sub split_len {
  my ($len, $string) = @_;
  $string =~ m/.{1,$len}/g;
}

OUTPUT

ABCDEFG
HIJKLMN
OPQRSTU
VWXYZ



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to