in reply to Searching Files

Other people have already posted reasonable solutions so I'll concentrate on the meta-problem here.

You keep posting questions like this, "I have file of format x with data y and I need to sort/count/total/modify it in way z". I think what you really need to do is likely to involve getting a book on SQL and installing a database. All these problems could have been solved in much more effective ways using simple SQL statements.

In this particular instance, what the poor monks responding to your question took 10+ lines to do could be done in one:

select,count(*) from datastore as n1, datastore as n2 where n2 and n2.yearmonth=200207 and n1.yearmonth<200207 group b +y;

Databases really come into their own with tabular data like this and if you're working with it a lot I *highly* recommend getting the necessary books, reading the online tutorials and installing one of the freely available RDBMSs (MySQL, PostGreSQL). That way the only thing you'll have to get perl to do is load the data in, from there you can play to your hearts content.