Do you see any pitfall if i remove the 'm' on the regexp
And use $string =~ /.{1,$len}/g; instead ?

I have tried without the 'm', it works as well :-)

Thanks to all of you by the way...

José.

-----Original Message-----
From: Rob Dixon [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 17, 2003 5:28 PM
To: [EMAIL PROTECTED]
Subject: Re: Split based on length



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]



**** DISCLAIMER ****

"This e-mail and any attachment thereto may contain information which is confidential 
and/or protected by intellectual property rights and are intended for the sole use of 
the recipient(s) named above. 
Any use of the information contained herein (including, but not limited to, total or 
partial reproduction, communication or distribution in any form) by other persons than 
the designated recipient(s) is prohibited. 
If you have received this e-mail in error, please notify the sender either by 
telephone or by e-mail and delete the material from any computer".

Thank you for your cooperation.

For further information about Proximus mobile phone services please see our website at 
http://www.proximus.be or refer to any Proximus agent.


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

Reply via email to