Hi,
to emphasis the two aspects of the function you could do a grep (removing totally empty list elements) with a map (removing leading and trailing whitespace).
UPDATE: Taste is hardly debatable, but performance often not:
use strict; use warnings; use Data::Dumper; use Benchmark qw(cmpthese); # Returns non-empty strings with leading and trailing spaces removed. sub mytrim_nonempties { grep { $_ ne '' } map { s/^\s+//; s/\s+$//; $_ } @_; } sub trim_nonempties { ## no critic (ProhibitMutatingListFunctions) grep { !/^\s*$/ && s/^\s*|\s*$//g } @_; ## use critic } my @x = ( ' hello ', "\thello again\t", '', " ", " \t", 'jock', "\t ab +c", "def \t " ); cmpthese(1000000, { 'myfunc' => sub { my @y = mytrim_nonempties(@x); }, 'yourfunc' => sub { my @y = trim_nonempties(@x); }, });
McA
In reply to Re: Code review: function to trim whitespace from all items in a list
by McA
in thread Code review: function to trim whitespace from all items in a list
by eyepopslikeamosquito
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |