2013-02-18 62 views
0

在我的代码中,我将一个可变光盘分配给我的Linux系统上的命令disc的结果。这将输出字符串研究Chomp将字符串更改为1

my $disc = `disc`; 
print "$disc\n"; 
$disc = chomp($disc); 
print "$disc\n"; 

然而,当我使用格格剥离从它改变了串1.这里是输出

RESEARCH 

1 

这是怎么回事字符串换行符?

回答

7

perldoc -f chomp

chomp VARIABLE 
chomp(LIST) 
chomp This safer version of "chop" removes any trailing string that 
     corresponds to the current value of $/ (also known as 
     $INPUT_RECORD_SEPARATOR in the "English" module). It returns the 
     total number of characters removed from all its arguments. 

的正确用法是简单地提供一个变量或列表将在地方被改变。你使用的返回值是它“参数化”参数列表的次数。例如。

chomp $disc; 

甚至:

chomp(my $disc = `disc`); 

例如,你可以的Chomp整个数组或列表,例如:

my @file = <$fh>;   # read a whole file 
my $count = chomp(@file); # counts how many lines were chomped 

当然,有一个标参数,格格回报值可以只有1或0.

2

仅仅使用chomp $disc没有任何作用,因为chomp返回删除的字符数。

+0

'$格格也disc'返回1个 – moadeep 2013-02-18 12:02:46

+0

@moadeep:是的,当然,它会返回删除的字符数。只需使用'chomp $ disc;' – Toto 2013-02-18 12:04:08

1

避免将chomp结果指定给变量:

$disc = chomp($disc); 

使用:

chomp($disc); 

这是因为格格修改定的字符串,返回其所有参数删除的字符总数