|Perl: the Markov chain saw|
as noted above, I have suggested yesterday four nested foreach loops in my answer to your cross post on the DevShed forum:
And I believe it is probably the best solution: clean, easy to read, easy to understand and efficient.
For the fun of it, however, let me suggest a kind of "Schwartzian" solution, which could also be called a "Lisp written in Perl" solution (or Scheme, or Haskell, or whatever functional language written in Perl):
(I have added spaces between the fields to make the output easier to read.) This prints (I copy only the beginning and the end of the 240-line printout):
The beauty (and fun) of this solution is of course that it holds in just one line of code (in pure Perl, without using a module to do the work behind the scene).
There are a number of downsides to this solution, however: first, it is far less easy to understand, it is actually hardly readable. Even though I just wrote it, I am not sure how I would explain it to make it clearly understandable.
To tell the truth, it was also a bit complicated for me to write it: it took me about 3 minutes to write the foreach nested loops above (which I did not even need to test), and and it took me slightly more than half an hour to get the nested map function calls to work correctly. So, in brief, this was just for the fun, I do not recommend this solution.