Certainly code would allow more explicit help; but you might want to investigate the DBD::CSV or DBD::AnyData modules. They allow you treat all sorts of data as a database and to perform SQL operations on those data.
I've been working with DBD::CSV a lot recently. It's a very handy tool to have. Even if it doesn't solve your immediate problem, I think you might want to know about it anyway.