[Israel.pm] splitting a string
Michael Spector
michael at zend.com
Tue Sep 26 10:23:01 EEST 2006
# something like:
sub efficient($$) {
my ($str, $n) = @_;
my $len = length($str);
my @res = ();
for (my $i = 0; $i < $len; $i += $n) {
push (@res, substr($str, $i, $n));
}
return @res;
}
sub elegant($$) {
my ($str, $n) = @_;
my $p = "." x $n;
return ($str =~ /$p/g);
}
On Tue, 26 Sep 2006, Levenglick Dov-RM07994 wrote:
LD>>Hi,
LD>>I am looking for the most efficient and most elegant (not necessarily
LD>>the same) way of splitting a string in to chunks of a given length.
LD>>E.g. if I have a string '123456' which I want to split in to two chunks
LD>>of 3 characters, I should have ('123', '456') when done.
LD>>
--
Best regards,
Michael
More information about the Perl
mailing list