2008-10-21 61 views

回答

70
perldoc -f length 

    length EXPR 
    length Returns the length in characters of the value of EXPR. If EXPR is 
      omitted, returns length of $_. Note that this cannot be used on an 
      entire array or hash to find out how many elements these have. For 
      that, use "scalar @array" and "scalar keys %hash" respectively. 

      Note the characters: if the EXPR is in Unicode, you will get the num- 
      ber of characters, not the number of bytes. To get the length in 
      bytes, use "do { use bytes; length(EXPR) }", see bytes. 
+0

謝謝!輕鬆代表你! :) – Kip 2008-10-21 20:43:12

+0

只是叫我「快速繪製」。 – 2008-10-21 20:43:50

41

雖然「長度()」是應該在任何理智的代碼中使用的正確答案,Abigail's length horror應當提到,如果只爲Perl知識的緣故。

基本上,訣竅在於使用包羅萬象的轉換操作符的返回值:

print "foo" =~ y===c; # prints 3 

Ÿ/// C替換所有字符本身(感謝補充選項「C」),並返回被替換的字符數(有效的是字符串長度)。

-2

length()功能:

$string ='String Name'; 
$size=length($string); 
相關問題