in reply to Re^2: Buffering and output help needed
in thread Buffering and output help needed

A return() statement exits the subroutine. That means you loop won't be completed, since incStart() will stop and return at the first value that matches ( $x % $_[2] ) == 0.

You'll want to create a list of matching variables to return. You could do it a bit like this: (code clarified a bit)

use strict; use warnings; sub incStart { my ($start,$end,$mod) = @_; my @result; for ( my $x = $start ; $x < $end ; $x++ ) { if ( ( $x % $mod ) == 0 ) { push @result,$x; # append $x to result list } } return @result; # <- return AFTER completing the loop } print "test "; foreach my $result (incStart(0,10,3)) { print "$result\n"; }

Please also be very careful about where you define variables and use strict and warnings.

Replies are listed 'Best First'.
Re^4: Buffering and output help needed
by richill (Monk) on Oct 07, 2006 at 13:42 UTC
    You are right, i needed to local scope the varibles in the sub routine, name them more clearly.

    I'd remembered about return exiting the sub routine and just finshed altering my code so that it worked like yours but with poor varible scoping and naming.

    It was good to see the difference.