in reply to Counting in loop help!
Perhaps using a state variable within the sub will work for you:
use Modern::Perl; for ( 1 .. 10 ) { my @names = (qw/Fred Barney Wilma Betty Bam-Bam Perl Java C++ Ruby Lisp/) [ 0 .. int( rand(10) ) ]; my ( $how_many, $count ) = check_names(@names); say "How Many Names: $how_many\nCount: $count\n"; } sub check_names { my (@names) = @_; state $count = 0; $count++; my $how_many_names_total = @names; return $how_many_names_total, $count; }
Output:
How Many Names: 1 Count: 1 How Many Names: 3 Count: 2 How Many Names: 5 Count: 3 How Many Names: 9 Count: 4 How Many Names: 1 Count: 5 How Many Names: 5 Count: 6 How Many Names: 5 Count: 7 How Many Names: 9 Count: 8 How Many Names: 2 Count: 9 How Many Names: 7 Count: 10
Hope this helps!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Counting in loop help!
by Anonymous Monk on Jul 27, 2012 at 13:25 UTC |