用perl怎么计算一个文本中的每一行包含另一文本的单词数.例如,文本1:hello nice to meet you how are you thank you where are you from 文本2:niceyoulove我想得到的结果是文本1中第一行包含2个文本2中单词,
来源:学生作业帮助网 编辑:六六作业网 时间:2025/02/03 16:59:00
用perl怎么计算一个文本中的每一行包含另一文本的单词数.例如,文本1:hello nice to meet you how are you thank you where are you from 文本2:niceyoulove我想得到的结果是文本1中第一行包含2个文本2中单词,
用perl怎么计算一个文本中的每一行包含另一文本的单词数.
例如,文本1:hello nice to meet you how are you thank you
where are you from
文本2:nice
you
love
我想得到的结果是文本1中第一行包含2个文本2中单词,第二行包含1个
用perl怎么计算一个文本中的每一行包含另一文本的单词数.例如,文本1:hello nice to meet you how are you thank you where are you from 文本2:niceyoulove我想得到的结果是文本1中第一行包含2个文本2中单词,
第2个文本是每行只包含一个单词吗?
先读文件2,每行存到数组1;
读文件1,构建哈希,行数为key,每行对数组进行循环,正则匹配:$line =~ /\b$word\b/; 如果匹配,这个行的hash值就加1。
我假设你第二个文件中的单词没有重复,第一个文件每行的重复单词也只算出现一次;
这些细节你自己改就行了。...
全部展开
先读文件2,每行存到数组1;
读文件1,构建哈希,行数为key,每行对数组进行循环,正则匹配:$line =~ /\b$word\b/; 如果匹配,这个行的hash值就加1。
我假设你第二个文件中的单词没有重复,第一个文件每行的重复单词也只算出现一次;
这些细节你自己改就行了。
收起
用法 filter.pl 1.txt 2.txt
#!/usr/bin/perl
open myfile,@ARGV[0];
open filter,@ARGV[1];
@myfilelist=
@filterlist=
foreach $myfilelist(@myfilelist)
{
$...
全部展开
用法 filter.pl 1.txt 2.txt
#!/usr/bin/perl
open myfile,@ARGV[0];
open filter,@ARGV[1];
@myfilelist=
@filterlist=
foreach $myfilelist(@myfilelist)
{
$count=0;
foreach $filterlist(@filterlist)
{
chomp $filterlist;
if($myfilelist=~/$filterlist/i) {$count++;}
}
print $myfilelist."=".$count."\n";
}
收起
for $k(split /\n/,`cat text2`){ $line_no=0; for $l(split /\n/,`cat txt1`){ $line_no++; @arr=$l=~/\s+/; @match=grep {/^$k$/} @arr; print qq/$k appears $match times in line $line_no\n/; } }