http://qs321.pair.com?node_id=1030976


in reply to How to keep running total?

First of all I don't see a reason to use a state machine. You just have lots of lines and a special action to do when the date changes. So I would throw out the variable $state completely

Also there is no reason for the counting to be done in an else-clause. You want to count every line.

To avoid printing the first time (when you haven't counted anything yet) just check for the empty string in date

If you want to try it for yourself don't reveal the spoiler, otherwise here is how the loop could be done (untested):