in reply to If condition logic yielding partially correct output

Only use != for numeric scalars. So
if (($key ne "") && ($key !~ /^[0.]+$/ ))){
If you don't know before hand which it is, treat it as a string, downside of weak typing for those asking about relative merits of Perl on whatever course you're doing ;)

Update: Actually scratch all that JavaFan's answer is a much better solution