I wasn't sure about the code from the mailing list either (like where did the $i come from etc) but I thought I would post it because it seems to be a decent attempt.
The error I was getting with the mailing list code (beside not initializing the $i) was "Type of arg 1 to values must be hash (not hash element)".
I will give it a try with your suggestions, but any more are welcome for the optimum way to do this. Thanks!