in reply to program doesnt work for all elements of the array
This is going to sound old-fashioned, but I'd recommend you spend some time in the Perl debugger. You can try out individual syntaxes, dump out variables, and get a feel for the language.
For example, you have syntax like @words1$a = $name;; it looks like you're trying to add $name to the words1 array at index $a. You haven't done this before, because that's the wrong syntax; and in any case, push is normally used to add elements to an array. Also, $a and $b are special variables used only in sort blocks -- it's not forbidden to use them, but it's considered bad style.
It's also handy to write yourself a comment or two so you have a good idea of what each piece of code is supposed to do. This will also help others understand what the code is meant to do.
O'Reilly's Learning Perl is a great starter book for this language.
Alex / talexb / Toronto
Thanks PJ. We owe you so much. Groklaw -- RIP -- 2003 to 2013.