You need to post a complete (but brief) example. The
@n = grep defined, @n should work for what you are describing. Your examples have no input and no output, so we can not tell where you are going wrong.
Mr.Muskrat's answer above is a complete example with input and output. Can you post something similar that demonstrates the problem? At a guess, maybe you have a typo and need
strict.