in reply to Is there a way more easy?

Hi,
using some functional programming to create an iterator you can end up with something like this:
use Modern::Perl; #my $alphabet = ['a'..'z', 'A'..'Z']; #my $word_length = 6; my $alphabet = ['a'..'b', '_']; my $word_length = 3; sub create_dictionary { my ($alphabet, $word_length) = @_; my @odometer = (0) x $word_length; my $len_alpha = @$alphabet; my $exhausted = 0; return sub { my $str = undef; if (!$exhausted) { $str = join('', map { $alphabet->[$_] } @odometer); my $add = 1; @odometer = map { $_+=$add; $add = $_ >= $len_alpha ? 1:0 +; $_% $len_alpha } @odometer; $exhausted = $add; # last carry == 1 => exhausted! } return $str; }; } my $dict = create_dictionary($alphabet, $word_length); while (my $word = $dict->()) { say $word; }

I got the odometer idea (if I remember correctly) from "higher order perl" http://hop.perl.plover.com/.

k

Replies are listed 'Best First'.
Re^2: Is there a way more easy?
by tobyink (Canon) on Jun 24, 2012 at 16:28 UTC

    List::MapMulti uses an odometer iterator internally.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'