Please use the code tags when posting code. I added them for you in this case, but please remember to use them in the future.

First, you need to work on using proper code indentation. Proper indentation makes the code easier to read, understand, and maintain.

Second recommendation would be to use the DBI module when interacting with databases instead of using shell commands. The DBI module will give you greater flexibility and control over the db interaction and would allow you to retrieve the records and build the hash in 1 simple statement.

Then loop over the remaining array elements and split the line into its 3 fields/vars and use those 3 vars to assign that row of data to a HoH (Hash of Hashes) or AoH (Array of Hashes). The choice of data structure will depend on the name field. If there is a possibility of having duplicate names, then you'll want to use the AoH, otherwise I'd use a HoH.

I'm describing what needs to be done rather than giving the complete code for 2 reasons. 1) You've posted this in the Intermediate area which make us assume you have some Perl experience. 2) I get the feeling that this is a class homework assignment which you need to work out.