I can't see anything physically wrong with the code you last posted. I would confirm in your data file that the MiddleName is even specified. Another possibility is the datasource isn't properly formatted, it would be very easy to miss a tab.
You should start at the first point when you get the data and trace it through the execution of your program. Before and after each step where it could be modified, you should check it and make sure it's correct. The debugger or a few print statements should help you trace where the problem is.
By the way, in the last code you posted, there were two lines of code that weren't being used for anything, as far as I could determine, the declaration of %data and %ref.