sonyet 发表于 2018-9-1 06:32:24

perl计算单词出现次数

  *root x 0 0 root root /bin/bash
  bin x 1 1 bin /bin /sbin/nologin
  daemon x 2 2 daemon /sbin /sbin/nologin
  adm x 3 4 adm /var/adm /sbin/nologin
  lp x 4 7 lp /var/spool/lpd /sbin/nologin
  sync x 5 0 sync /sbin /bin/sync
  shutdown x 6 0 shutdown /sbin /sbin/shutdown
  halt x 7 0 halt /sbin /sbin/halt
  mail x 8 12 mail /var/spool/mail /sbin/nologin
  news x 9 13 news /etc/news
  ##########################################################
  #!/usr/bin/perl
  #use strict;
  #use warnings;
  use 5.010;
  my %count;
  while (){
  foreach (split){
  $total++;
  next if /\W+/;
  $valid++;
  $count{$_}++;
  }
  }
  print "total is: $total & valid is: $valid & count is: $count{$_}\n";
  foreach $word (sort keys %count) {
  print "$word was seen $count{$word} times.\n";
  }

页: [1]
查看完整版本: perl计算单词出现次数