|The stupid question is the question not asked|
The close-curly on Ln 15 terminates your if clause... but there's no body to the if block. No code means the first if test is a nullity.
If that first conditional fails, the code falls thru to the elsif.
Pls explain why your code at Ln 40, open too,">out.txt"; # open file for counts to be depposited to, is unlike your other (reasonably well-formed opens.
It appears that you're using neither strict nor warnings. Both would help you find your own syntax mistakes. Your localization (or Not) of variables is inconsistent. Your $ARG variable tromps all over it's conventional usage:
Your indentation is idiosyncratic (at the kindest) and erratic. Single-tab (or 2 to 4 space) indents for each level of curlies would make this code far easier to read.
... and so far, nothing here explicitly addresses your question. That's deliberate. Please, when you post code, make sure it's runnable (or at least passes a compile test with strict in force. And, please, put your data inside code tags, so there's no question about the actual content of the data.. If you fix these problems, you'll find the Monks most generous in helping you learn.