ivaneeo's blog

          自由的力量,自由的生活。

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

          常用鏈接

          留言簿(32)

          我參與的團(tuán)隊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          1.split函數(shù)

          ???%seen = ( );
          ???$string = "an apple a day";
          ???foreach $char (split //, $string) {
          ???????$seen{$char}++;
          ???}

          2./(.)/g 但是(.)永遠(yuǎn)不會是newline
          ???%seen = ( );
          ???$string = "an apple a day";
          ???while ($string =~ /(.)/g) {
          ???????$seen{$1}++;
          ???}

          3.unpack("C*")也可以逐個處理字符:(這個例子是累加字符串里每個字符ascii碼的累加值)
          ???$sum = 0;
          ???foreach $byteval (unpack("C*", $string)) {
          ???????$sum += $byteval;
          ???}
          ???print "sum is $sum\n";
          ???# prints "1248" if $string was "an apple a day"

          $sum = unpack("%32C*", $string); #這個方法比上面更快,這個返回32位的checksum值.

          4 .<>是默認(rèn)的輸入流,其實就是ARGV.
          ?????這個模擬sysv的checksum程序:
          ???#!/usr/bin/perl
          ???# sum - compute 16-bit checksum of all input files
          ???$checksum = 0;
          ???while (<>) { $checksum += unpack("%16C*", $_) }
          ???$checksum %= (2 ** 16) - 1;
          ???print "$checksum\n";
          ???

          ???Here's an example of its use:

          ???% perl sum /etc/termcap
          ???1510


          ???If you have the GNU version of sum, you'll need to call it with the —sysv option to get the same answer on the same file.

          ???% sum --sysv /etc/termcap
          ???1510 851 /etc/termcap

          一個詳細(xì)的例子:



          #!/usr/bin/perl # slowcat - emulate a s l o w line printer # usage: slowcat [-DELAY] [files ...] $DELAY = ($ARGV[0] =~ /^-([.\d]+)/) ? (shift, $1) : 1; #這里[.]取消了.的特殊性。使其為一般意義。shift移除了@ARGV第一個變量和長度減一。 $| = 1; #不為0就強行清空輸出或打印。 while (<>) { #<>為@ARGV指定的文件句柄 for (split(//)) { print; select(undef,undef,undef, 0.005 * $DELAY); #select函數(shù)設(shè)置屏幕輸出。這里是設(shè)置延遲。 } }












          posted on 2006-04-09 11:16 ivaneeo 閱讀(400) 評論(0)  編輯  收藏 所屬分類: perl-殺雞就要用牛刀
          主站蜘蛛池模板: 睢宁县| 和静县| 屏东县| 高平市| 曲阳县| 绿春县| 清流县| 高碑店市| 阳西县| 枝江市| 林芝县| 且末县| 诸暨市| 绍兴市| 远安县| 呈贡县| 苗栗县| 宁晋县| 新竹县| SHOW| 五家渠市| 穆棱市| 铜鼓县| 松原市| 黄陵县| 东兴市| 通城县| 阿荣旗| 嵩明县| 嘉荫县| 册亨县| 庆元县| 库车县| 平顶山市| 扶余县| 咸宁市| 灵台县| 全椒县| 惠东县| 车致| 石林|