jZed, my first thought was "this would be easy if it were SQL".
But the example data is really brain-dead; the real data is interspersed throughout a gigantic text file, and normalizing it for a database isn't really an option.
Update: jZed's reply is right on. I didn't read closely enough. In fact, I didn't realize DBI was capable of treating an arrayref as a database and getting to it via SQL.
I'm knee-deep in the Guttman/Rosler paper at the moment (good information there), but this is definitely worth exploring. Soon.