Author image Hauke D
and 1 contributors


Algorithm::Odometer::Gray - Generate a "n-ary" / "non-Boolean" Gray code sequence (Cartesian product / product set)


 use Algorithm::Odometer::Gray;
 my $odometer = Algorithm::Odometer::Gray->new( ['a','b','c'], [1,2] );
 print "$_ " while <$odometer>;
 print "\n";
 # => prints the sequence "a1 b1 c1 c2 b2 a2"


This class implements the permutation algorithm described in [1] and [2]. It differs from Algorithm::Odometer::Tiny only in the order of the generated sequence, so for all details about usage etc. please see Algorithm::Odometer::Tiny.


The following wheels:

 ["Hello","Hi"], ["World","this is"], ["a test.","cool!"]

produce this sequence:

 ("Hello", "World",   "a test.")
 ("Hi",    "World",   "a test.")
 ("Hi",    "this is", "a test.")
 ("Hello", "this is", "a test.")
 ("Hello", "this is", "cool!")
 ("Hi",    "this is", "cool!")
 ("Hi",    "World",   "cool!")
 ("Hello", "World",   "cool!")

Note how from each item to the next, only one of the wheels changes, even when the sequence ends and wraps around to the beginning.

See Also


  1. Knuth's "The Art of Computer Programming", Section "Generating all n-tuples", Algorithm "Loopless reflected mixed-radix Gray generation".

  2. Bird, Richard. (2006). Loopless Functional Algorithms. 4014. 90-114. 10.1007/11783596_9. Section 9.5. "Non-binary Gray codes", Algorithm C.

Author, Copyright, and License

Copyright (c) 2019 Hauke Daempfling (

This library is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.

For more information see the Perl Artistic License, which should have been distributed with your copy of Perl. Try the command perldoc perlartistic or see