in reply to In need of a sequence iterator.
How about:
sub genIterator { my ($len,$count) = @_; my @list = (); return sub { if (@list == 0 || @list < $len) { push @list, 0; } else { $list[-1]++; while (@list && $list[-1] >= $count) { pop @list; $list[-1]++ if @list; } } return @list ? join("", @list) : undef; } }
Note that the code to iterate should be:
my $iter = genIterator(4,3); print while defined($_ = $iter->());
since 0 evaluates as false.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: In need of a sequence iterator.
by BrowserUk (Patriarch) on Dec 02, 2004 at 08:04 UTC | |
by !1 (Hermit) on Dec 02, 2004 at 16:30 UTC | |
Re^2: In need of a sequence iterator.
by hv (Prior) on Dec 02, 2004 at 13:27 UTC | |
by BrowserUk (Patriarch) on Dec 02, 2004 at 17:50 UTC | |
by hv (Prior) on Dec 03, 2004 at 12:53 UTC |
In Section
Seekers of Perl Wisdom