Let Lua do the work. See
http://lua-users.org/lists/lua-l/2006-12/msg00575.html and others in
that thread for instance.
From a brief look at Mr. Ramseys' answer, creating a similar solution might be possible with the Moses library. The functions containsKeys, sameKeys and map all look like they offer some compelling tools...