in reply to Eliminating substr warnings

Here's my take (which doesn't require turning off warnings):
use strict; use warnings; my $i = 0; my $size = 10; my $string = 'some long string'; my @substrings; my $len = length($string); while($i <= $len) { push(@substrings, substr($string, $i, $size)); $i += $size; } $" = "\n"; print "@substrings\n";
UPDATE: copied the "remove temp vars" from above posts.

Replies are listed 'Best First'.
Re^2: Eliminating substr warnings
by GreyGlass (Sexton) on Aug 13, 2004 at 03:16 UTC
    Density to time conversion:

    If you are as slow as I am, you may be wondering why this works and the original produced a warning.

    It's becuase the original produced the warning _after_ the last non-empty string was extracted. No warning is produced when the offset is equal to the string length, hence no warning when the length is a multiple of $size

Re^2: Eliminating substr warnings
by Roy Johnson (Monsignor) on Aug 13, 2004 at 20:35 UTC
    This is a good place for map:
    use warnings; use strict; my $str = 'abcdefghijklmnopqrstuvwxyz'; my $size = 10; my @subs = map substr($str, $_*$size, $size), 0..(length($str)-1)/$siz +e; print join("\n", @subs), "\n";

    Caution: Contents may have been coded under pressure.