使用Split&映射在Perl中修剪 [英] Map with Split & Trim in Perl
本文介绍了使用Split&映射在Perl中修剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用带有split功能的map修剪成分:$ a,$ b,$ c和$ d;的$ line?
How do I use map with the split function to trim the constituents: $a, $b, $c and $d; of $line?
my ($a, $b, $c, $d, $e) = split(/\t/, $line);
# Perl trim function to remove whitespace from the start and end of the string
sub trim($)
{
my $string = shift;
$string =~ s/^\s+//;
$string =~ s/\s+$//;
return $string;
}
推荐答案
除非您需要,否则请勿在函数上使用($)
原型.
Don't use prototypes the ($)
on your function unless you need them.
my ( $a, $b, $c, $d, $e ) =
map {s/^\s+|\s+$//g; $_} ## Notice the `, $_` this is common
, split(/\t/, $line, 5)
;
请不要忘记上面的s///
返回替换计数-不是$_
.所以,我们明确地做到这一点.
Don't forget in the above s///
returns the replacement count -- not $_
. So, we do that explicitly.
或更简单地说:
my @values = map {s/^\s+|\s+$//g; $_}, split(/\t/, $line, 5), $line
这篇关于使用Split&映射在Perl中修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文