在我的代碼中,我將一個可變光盤分配給我的Linux系統上的命令disc
的結果。這將輸出字符串研究Chomp將字符串更改爲1
my $disc = `disc`;
print "$disc\n";
$disc = chomp($disc);
print "$disc\n";
然而,當我使用格格剝離從它改變了串1.這裏是輸出
RESEARCH
1
這是怎麼回事字符串換行符?
在我的代碼中,我將一個可變光盤分配給我的Linux系統上的命令disc
的結果。這將輸出字符串研究Chomp將字符串更改爲1
my $disc = `disc`;
print "$disc\n";
$disc = chomp($disc);
print "$disc\n";
然而,當我使用格格剝離從它改變了串1.這裏是輸出
RESEARCH
1
這是怎麼回事字符串換行符?
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.
僅僅使用chomp $disc
沒有任何作用,因爲chomp返回刪除的字符數。
避免將chomp結果指定給變量:
$disc = chomp($disc);
使用:
chomp($disc);
這是因爲格格修改定的字符串,返回其所有參數刪除的字符總數
'$格格也disc'返回1個 – moadeep 2013-02-18 12:02:46
@moadeep:是的,當然,它會返回刪除的字符數。只需使用'chomp $ disc;' – Toto 2013-02-18 12:04:08