How do I remove all undefs from array?

flohei picture flohei · Jun 20, 2012 · Viewed 8.5k times · Source

While reading from a configuration file in Perl there might be cases when a line is invalid and it does not need to get added to my array of valid lines. Since I'm using a for loop here, even the invalid lines create an undef entry. How can I remove all them afterwards?

Thanks!

Answer

mob picture mob · Jun 20, 2012
@array = grep defined, @array;