in reply to Re: In need of a sequence iterator.
in thread In need of a sequence iterator.

As far as I can see this is just counting in base $count, in which case I'd probably use a string instead:

sub genIterator { my($len, $count) = @_; my $cur = ''; my $lastdig = $count - 1; return sub { if (length($cur) < $len) { $cur .= '0'; } else { $cur =~ s/([^$lastdig])$lastdig*\z/$1 + 1/e or $cur = undef; } $cur; } }

Of course this assumes $count doesn't exceed 10; this approach gets a bit more complex if you need to deal with larger bases.

Hugo

Replies are listed 'Best First'.
Re^3: In need of a sequence iterator.
by BrowserUk (Patriarch) on Dec 02, 2004 at 17:50 UTC

    The $count can get upto 255 (and theoretically beyond with unicode). The numbers are eventually packed to form a binary string of codepoints that increment, magic string auto-increment style, but starting at chr(0) and modulo alphabet size.

    I'm currently limiting my thinking to 8-bit ascii bytes, but notionally the alphabet could be larger with unicode.

    The actual keys involved (made up from an arbitrary alphabet) are translated to chr(0) ... chr(alphabet max).


    Examine what is said, not who speaks.
    "But you should never overestimate the ingenuity of the sceptics to come up with a counter-argument." -Myles Allen
    "Think for yourself!" - Abigail        "Time is a poor substitute for thought"--theorbtwo         "Efficiency is intelligent laziness." -David Dunham
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon

      Ah ok, well you could use the string approach directly on the packed strings by changing the replace to /chr(ord($1)+1)/e or use a hash to describe successorship in the arbitrary alphabet:

      my $alphabet = "0123456789ABCDEF"; my %successor = $alphabet =~ /(.)(?=(.))/g; my $zero = substr($alphabet, 0, 1); ... sub genIterator { my($len, $count) = @_; my $cur = ''; my $lastdig = substr($alphabet, $count - 1, 1); return sub { if (length($cur) < $len) { $cur .= $zero; } else { $cur =~ s/([^$lastdig])$lastdig*\z/$successor{$1}/ or $cur = undef; } $cur; } }

      Hugo