2012年9月30日 星期日

[ Perl 函數 ] substr : 子字串的切割


來源自 這裡
說明 :
我們可以利用函數 substr 來取出某個字串內的子字串, 透過不訂長度的參數提供客製化的功能; 甚至我們能透過它將特定位置的子字串以提供的字串進行取代.

語法 :
substr EXPR,OFFSET,LENGTH,REPLACEMENT
substr EXPR,OFFSET,LENGTH
* substr EXPR,OFFSET 

substr 會對參數 EXPR 進行處理.
- 如果只提供 OFFSET 參數, 則子字串的範圍從 OFFSET 的位置到字串結束; 如果 OFFSET 是負數, 則從字串尾端數回來.
- 如果提供 OFFSETLENGTH, 則從 OFFSET 的位置取長度為 LENGTH 的子字串; 如果 LENGTH 為負數, 說明從字串結尾往前數長度為 LENGTH 不要算進子字串.
- 如果提供 OFFSETLENGTHREPLACEMENT, 則將子字串以 REPLACEMENT 取代.

簡單範例如下 :
  1. my $s = "The black cat climbed the green tree";  
  2. my $color  = substr $s, 45;      # black  
  3. print "\$color=$color\n";  
  4.   
  5. my $middle = substr $s, 4, -11;    # black cat climbed the  
  6. print "\$middle=$middle\n";  
  7.   
  8. my $end    = substr $s, 14;        # climbed the green tree  
  9. print "\$end=$end\n";  
  10.   
  11. my $tail   = substr $s, -4;        # tree  
  12. print "\$tail=$tail\n";  
  13.   
  14. my $z      = substr $s, -42;     # tr  
  15. print "\$z=$z\n";  
使用範例 :
底下透過範例說明使用方法.

- Example 1a. Supply an expression and a positive offset value.
在這個範例將字串 $string 從 offset=4 的位置一直到字串結尾擷取到另一個變數 $fragment. 代碼如下:
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. my $fragment =  substr $string, 4;  
  7. print "  string: <$string>\n";  
  8. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good people to come to the aid of their party>
fragment: 
<is the time for all good people to come to the aid of their party>

- Example 1b. Supply an expression and a positive offset value.
在這個範例中, 透過函數 index() 找到 'people' 所在位置, 並將之指定給 OFFSET. 如此我們可以取得從 'people' 算起一直到字串結束的子字串:
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. # Find the location of the substring 'people'  
  6. my $string = 'Now is the time for all good people to come to the aid of their party';  
  7. my $fragment =  substr $string, index($string, 'people');  
  8. print "  string: <$string>\n";  
  9. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good people to come to the aid of their party>
fragment: 
<people to come to the aid of their party>

- Example 2a. Supply an expression, a positive offset value and a length
透過 LENGTH 參數, 我們可以決定子字串的長度. 下面從 OFFSET=7 的位置上取出長度為 8 的子字串:
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. my $length = 8;  
  7. my $fragment =  substr $string, 7, $length;  
  8. print "  string: <$string>\n";  
  9. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good people to come to the aid of their party>
fragment: 
<the time>

- Example 2b. Supply an expression, a negative offset value and a length
如果我們的 OFFSET 是負數, 說明從字串結果從字串尾往回數. 下面代碼從結尾往回數 16 個位置的 offset 上取長度為 10 的子字串:
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. my $length = 10;  
  7. my $fragment =  substr $string, -16, $length;  
  8. print "  string: <$string>\n";  
  9. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good people to come to the aid of their party>
fragment: 
<d of their>

- Example 2c. Supply an expression, a positive offset value and a negative length
如果參數 LENGTH 是負數, 說明有多少個長度從結尾的位置被 truncate 掉. 下面代碼取出從字串頭往前數 7 的到字串尾往回數 20 的子字串:
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. my $length = -20;  
  7. my $fragment =  substr $string, 7, $length;  
  8. print "  string: <$string>\n";  
  9. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good people to come to the aid of their party>
fragment: 
<the time for all good people to come to th>

- Example 3. An expression, an offset value, a length and a replacement value
如果有提供 REPLACEMENT 參數, 則子字串會被該參數取代. 下面透過函數 index() 找到 'people' 所在位置並且 'people' 的長度為6. 接著我們使用 REPLACEMENT 參數 將 'people' 取代成 'men':
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. my $fragment =  substr $string, index($string, 'people'), 6'men';  
  7. print "  string: <$string>\n";  
  8. print "fragment: <$fragment>\n";  
執行結果:
string: <Now is the time for all good men to come to the aid of their party>
fragment: 
<people>

可以看到原 $string 的 'people' 被替換成 'men', 而且 substr() 依舊返回 'people'.

- Example 4. Assigning to substr()
事實上 substr() 可以被當作 lvalue 使用, 所以你可以透過給定值給 substr() 取代 REPLACEMENT 的功能. 下面代碼將 'people' 子字串取代為 'women':
  1. #!/usr/bin/perl  
  2. use strict;  
  3. use warnings;  
  4.   
  5. my $string = 'Now is the time for all good people to come to the aid of their party';  
  6. substr($string, index($string, 'people'), 6) = 'women';  
  7. print "  string: <$string>\n";  
執行結果:
string: <Now is the time for all good women to come to the aid of their party>


補充說明 :
perl 學習手扎 > 第十二章 字串處理
perldoc.perl.org > functions > substr
stackoverflow > What are the uses of lvalue subroutines in Perl?
LValues are recommended to be avoided. They're fun and all, but they confuse new users who make assumptions about how all subs work, and thus it should be avoided in code when a better way is available...
This message was edited 24 times. Last update was at 01/10/2012 11:08:33

沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...