开发者

Can this be written in 1 or 2 lines without tmp variable?

I have this code

my $tmp = $q->param('owner');
$tmp =~ s/\s*//g; # remove white space from string
my @owners     = split ",", $tmp;

which开发者_开发问答 works, but it takes up 3 lines, and it seams very wrong to use a temporary variable.

Can it be done in fewer lines and without a temporary variable?


I'd write it like this:

my @owners = map { s/\s*//g; $_ } split ",", $q->param('owner');

Since you're taking all the whitespace out, it doesn't matter whether you do it to the input or the list.

Of course, I use map-ped substitutions often enough that I've got a sub called filter, which looks like this filter { s/\s*//g } ... which is basically the same.


my @owners = split /\s*,\s*/, $q->param('owner');


You can use:

my @owners = split(/\s*,\s*/, $q->param('owner');

But you will still have the spaces that are not around the ,


my @owners = $q->param( 'owner' ) =~ m{([^,\s]+)}g;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜