in reply to no grep match returns undefined?

When grep can't find anything, it returns an empty list in a list context, or 0 in a scalar context. Since the next operation is a list slice, the list slice is providing list context to grep, and thus gets an empty list. You're then taking the 0'th element of an empty list, which is defined to be undef, because whenever you access "beyond the end of the array", you get undef. This is not a "special case" for grep.

Really, it's a matter of understanding each step, and is desk-checkable before running. Don't throw code together. Build it.

-- Randal L. Schwartz, Perl hacker

  • Comment on •Re: no grep match returns undefined?