The explanation, I suspect, for the first part of your issue is that when you compare two non-numeric strings using ==, they both get converted to zero and thus are reported as equal. So if you're comparing strings you really need to use eq and not ==.