Perl 笔试题2 -- 统计单词频次

3 篇文章 0 订阅

Nvidia 2019 perl 笔试题

统计一个文件内单词的频次并排序

文本如下:

"ALL happy families resemble one another; every unhappy family is unhappy in its own way.
All was confusion in the house of Oblonskys. The wife had dicscovered that her husband was having an intrigue with a French governess who had been
in their employ, and the declared that the could not live in the same house with him. This condition of things had lasted now three days, and was causing
deep discomfort, not only to..."
happy? happy: [happy] {happy} "happy" 'happy'

代码

#!/usr/bin/perl

open(IN,"<word_frequance.txt") or die "file does not exist!";

while(<IN>){
    chomp;
    $_ =~ tr/a-zA-Z/ /cs;   #将除了字母之外的所有字符都换成一个空格
    $_ =~ s/^\s+//;   ## 丢弃前导空白符
    $_ =~ s/\s+$//;   ## 丢弃末尾空白符
    @words = split(/\s+/,$_);
    foreach $a (@words){
        $dict{lc($a)}++;     #创建字典时先将key小写
    }
}
close(IN);
foreach $word (sort keys %dict) {
        print "$word,$dict{$word}\n";  #打印出单词出现的个数
}

结果

a,1
all,2
an,1
and,2
another,1
been,1
causing,1
condition,1
confusion,1
could,1
days,1
declared,1
deep,1
dicscovered,1
discomfort,1
employ,1
every,1
families,1
family,1
french,1
governess,1
had,3
happy,7
having,1
her,1
him,1
house,2
husband,1
in,4
intrigue,1
is,1
its,1
lasted,1
live,1
not,2
now,1
oblonskys,1
of,2
one,1
only,1
own,1
resemble,1
same,1
that,2
the,5
their,1
things,1
this,1
three,1
to,1
unhappy,2
was,3
way,1
who,1
wife,1
with,2

注意事项

split 产生空元素

在按照/ /或者/\s+/来split字符串时,常会遇到莫名其妙多出来一个空元素的问题。

这是因为如果字符串开头就是空格,split会把开头的前导空白符(一个空字符)也算作一个元素。

如果要按照空格来split,有几种方法:

  1. split ' '或者直接用默认形式split,不加任何东西
    • split ' '是split的特殊情况,该格式是模拟awk的默认行为,所以在分割行为开始之前,会把字符串中的前导空格全部删除,然后再使用split /\s+/处理。
  2. 删除前导空白符,再用split(/\s+/,$_);
    • $_ =~ s/^\s+//; ## 丢弃前导空白符 $_ =~ s/\s+$//; ## 丢弃末尾空白符
PerlIO::via::QuotedPrint是Perl语言中的一个模块,用于在文件读写时对数据进行Quoted-Printable编码和解码。Quoted-Printable是一种用于在ASCII字符集中表示非ASCII字符的编码方式,常用于电子邮件和网络传输中。PerlIO::via::QuotedPrint模块提供了一种方便的方式,可以在文件读写时对数据进行Quoted-Printable编码和解码。 PerlIO::via::QuotedPrint模块的使用方法与其他PerlIO模块类似。可以使用open函数打开一个文件句柄,并指定PerlIO::via::QuotedPrint模块作为过滤器,从而实现对文件数据的编码和解码。 下面是一个使用PerlIO::via::QuotedPrint模块对文件进行Quoted-Printable编码和解码的例子: ``` use PerlIO::via::QuotedPrint; # Quoted-Printable编码 open(my $fh, '>:via(QuotedPrint)', 'file.txt'); print $fh "这是一段中文文本\n"; close($fh); # Quoted-Printable解码 open(my $fh, '<:via(QuotedPrint)', 'file.txt'); while(my $line = <$fh>) { print $line; } close($fh); ``` 在上面的例子中,我们使用了PerlIO::via::QuotedPrint模块对一个文件进行了编码和解码。在第一个open函数中,我们使用了':via(QuotedPrint)'指定了PerlIO::via::QuotedPrint模块作为过滤器,从而将写入文件的数据进行了Quoted-Printable编码。在第二个open函数中,我们同样使用了':via(QuotedPrint)'指定了PerlIO::via::QuotedPrint模块作为过滤器,从而将读取的数据进行了Quoted-Printable解码。 使用PerlIO::via::QuotedPrint模块可以方便地对文件数据进行Quoted-Printable编码和解码,从而满足不同应用场景中对数据编码的需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

love小酒窝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值