Mike's recommendation ended up satisfying all my test cases, so that's
good enough for me. I have no problem with the solution being ugly,
particularly since in a difficult problem like this, ugly becomes
fairly relative.

Thanks for your notes on using DBI for this, Kaspar. Such changes seem
like they're not necessary now but will definitely be important at
some point, sooner or later ...

Francis