good chemistry is complicated, and a little bit messy -LW |
|
PerlMonks |
•Re: no grep match returns undefined?by merlyn (Sage) |
on Sep 05, 2002 at 17:04 UTC ( [id://195445]=note: print w/replies, xml ) | Need Help?? |
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
In Section
Seekers of Perl Wisdom
|
|