ezekiel has asked for the wisdom of the Perl Monks concerning the following question:
I have a piece of code that gathers a set of substrings of fixed length from a longer string, thus:
my $i = 0; my $size = 10; while (my $substring = substr($string, $i, $size)) { push (@substrings, $substring); $i += $size }
This works fine, except that if the size of the segment does not evenly divide the size of the string, getting the last segment results in a warning message: "substr outside of string"
While this doesn't hurt anything, it offends my sense of order to see warnings like this where everything is working correctly :-) I could rewrite the code to use a for loop after working out the number of substrings based on the length of the string and the size of the substring, but I find this rather awkward and inelegant
Any suggestions for an elegant solution to this problem that avoids the error messages?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Eliminating substr warnings
by Zaxo (Archbishop) on Aug 13, 2004 at 00:53 UTC | |
by merlyn (Sage) on Aug 13, 2004 at 00:56 UTC | |
by GreyGlass (Sexton) on Aug 13, 2004 at 15:34 UTC | |
|
Re: Eliminating substr warnings
by Aristotle (Chancellor) on Aug 13, 2004 at 03:00 UTC | |
by thor (Priest) on Aug 13, 2004 at 04:29 UTC | |
|
Re: Eliminating substr warnings
by saberworks (Curate) on Aug 13, 2004 at 01:02 UTC | |
by GreyGlass (Sexton) on Aug 13, 2004 at 03:16 UTC | |
by Roy Johnson (Monsignor) on Aug 13, 2004 at 20:35 UTC |