First, apply the suggestions from toolic, ahmed and graff. In particular, please fix your indentation - as toolicsaid, perltidy will do that for you.
In addition:
You initialize $count to 1 to start, which will give you an off-by-one error. You mean 0.
You do not reinitialize $count before your M737 loop. That means this will count both M729 and M737.
Since your print statement is inside your for loop, you will print your results string many times - 6, 9, and 7 times respectively for the provided input. In addition, your output will be wrong every time for Q569 as all your prints will happen before you process any of your Q569s - it will print 0 each time.
Post code again if you have all implemented these changes and your code still doesn't function as expected.