in reply to Using split() to divide a string by length
I'm not sure split is the right choice for extracting fixed-length substrings. Isn't that really what substr is for (I mean, if you don't want to use unpack)?
sub split_len { ## split_len( $chars, $string[, $limit] ) ## - splits $string into chunks of $chars chars ## - limits number of segments returned to $limit, if provided my ($chars, $string) = @_; my ($i, @result); for ($i = 0; ($i+$chars) < length($string); $i+=$chars) { last if (defined $limit && @result >= $limit); push @result, substr($string, $i, $chars); } # deal with any short remainders return @result if (defined $limit && @result >= $limit); if ($i > length($string)-$chars) { push @result, substr($string, $i); } return @result; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Using split() to divide a string by length
by Hue-Bond (Priest) on Apr 19, 2006 at 10:18 UTC |