in reply to Algorithm for "Incrementing" strings

It's so simple it doesn't even need a sub:

#!/usr/bin/perl use strict; use warnings; my $consonants = 'BCDFGHJKLMNPQRSTVWXZ'; my @letters = split //, $consonants; my ($first, $last) = @letters[0, -1]; my %next; @next{'', @letters} = @letters; my $x = 'B'; for (1..450) { print "$x "; $x =~ s/([^$last]?)($last*)$/ $next{$1} . $first x length $2/ e; } print "\n";

Replies are listed 'Best First'.
Re^2: Algorithm for "Incrementing" strings
by Anonymous Monk on Feb 14, 2015 at 17:51 UTC

    And with only a few tweaks, we can decrement strings:

    #!/usr/bin/perl use strict; use warnings; my $consonants = 'BCDFGHJKLMNPQRSTVWXZ'; my @letters = split //, $consonants; my ($first, $last) = @letters[0, -1]; my %prev; @prev{@letters} = ('', @letters); my $x = 'BCD'; while( length $x ) { print "$x "; $x =~ s/(.)($first*)$/ $prev{$1} . $last x length $2 /e; } print "\n";
Re^2: Algorithm for "Incrementing" strings
by Anonymous Monk on Feb 14, 2015 at 05:21 UTC

    Oops, typo

    $x =~ s/([^$last]?)($last*)$/ $next{$1} . $first x length $2 /e;

      The general "increment a string" algorithm popped into my head much later so I checked back and was happy to see that it had already been posted. Here is my formulation of it that requires no set-up (easy for a human to compose when told what "digits" to use but not appropriate to use if the digits are not pre-set):

      s{([^Z])?(Z*)$}{ local $_ = ( $1 // 'Z' ) . $2; tr/BCDFGHJ-NP-TV-Z/CDFGHJ-NP-TV-ZB/; $_ }e;

      Decrement:

      s{(([^B])|B)(B*)$}{ local $_ = ( $2 // '' ) . $3; tr/CDFGHJ-NP-TV-ZB/BCDFGHJ-NP-TV-Z/; $_ }e;

      - tye