Monday, October 5, 2009

A journey through whitespace cleanup - Perl5 and split

Running through some older Perl5 code, I found someone using grep with a split to cleanup white space on some incoming data.

Found, this seemed confusing to me with lots of extra complexity:
my @line = grep { s/\s*$// } split /\|/, $_;


First Pass:
my @line = split /\|\s*/, $_;


But my guess is that all wrapping white space should be removed:
my @line = split /\s*\|\s*/, $_;


Even better (and more readable), use the Perl magic context:
my @line = split /\s*\|\s*/;


I would love to make that a cleaner regex but that will have to be another day...

No comments:

Post a Comment