in reply to i dont like the way my code looks but work

Your bug with grep is that you're assigning to @matches every time. Instead, the following will work:
push @matches, grep { $_ eq $item } keys %total;
Of course, the exists test is better yet, but that at least helps you out of your grep problem.

My criteria for good software:
  1. Does it work?
  2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?