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?
In reply to Eliminating substr warnings by ezekiel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |