Your code is a mess. Please provide a Short, Self-Contained, Correct Example.
Counting the frequency of words in Perl is a FAQ, best solved with a hash. See for example:
In reply to Re: Word Count and Match
by eyepopslikeamosquito
in thread Word Count and Match
by PilotinControl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |