$ cat tmp.txt
hello world
hello linux
hello perl
good perler
best linuxer
strong unixer
weak windower
$ perl -ne 'BEGIN {print "---统计文本中的单词数---\n"} END {print "测试文本中总共有 $t 个单词\n";} @word = /(\w+)/g; $t += @word' tmp.txt
---统计文本中的单词数---
测试文本中总共有 14 个单词
单词字符是“字母,下划线 以及数字”,因此 \w+ 可以匹配整个单词。另外,当一个数组直接赋值给变量时,该变量得到的是该数组的元素个数,如 $t += @word 所示。 |
欢迎光临 曲径通幽论坛 (http://www.groad.net/bbs/) | Powered by Discuz! X3.2 |