arrays - 比较两个文件以查找常见且唯一的条目
问题描述
我想比较两个文件中的条目。
第一个文件
ch1 12345 ch1 12900 Del
ch1 13000 ch1 13689 Del
ch2 14567 ch2 14905 Del
ch4 23001 ch4 26019 Del
第二个文件
ch1 12309 ch1 12865
ch2 14531 ch2 14871
ch3 16909 ch3 16990
ch4 45401 ch4 45810
第一个和第二个文件共有的条目(公共条目的条件在两侧至少有 50 个位置)
ch1 12345 ch1 12900 Del, ch1 12309 ch1 12865 ch2 14567 ch2 14905 Del, ch2 14531 ch2 14871
第一个文件独有的条目
ch1 13000 ch1 13689 Del ch4 23001 ch4 26019 Del
第二个文件独有的条目
ch3 16909 ch3 1699 ch4 45401 ch4 45810
我已经编写了一个 Perl 程序,在另一个数组中循环一个数组并识别公共条目,但是使用这种方法我无法识别唯一条目。
解决方案
您可以尝试以下程序。
如果您改变了双方必须发生多少次的想法,请更改$limit
变量的值。也许只有 1 就足够了?
use strict; use warnings;
# Count text occurrences in 2 files
my %counts; # Hash of arrays (occurrence counters)
sub inputProcess {
# Process input file
my $fn = shift; # File name
my $n = shift; # Counter index
open (my $fh, '<', $fn) or die "Can't open $fn: $!";
while (<$fh>) {
chomp;
$counts{$_}[$n]++;
}
}
sub readCounts {
# Read counters, substituting 0 if any does not exist
my $cnt = shift; # Reference to array of counters
my @v = (0, 0);
foreach my $i ( 0 .. $#v ) {
$v[$i] = $cnt->[$i] if exists $cnt->[$i];
}
return @v;
}
sub printUniq {
# Print unique entries for the given counter
my $myCnt = shift; # "My" counter
my $otherCnt = 1 - $myCnt; # The "other" counter
print "Unique entries from file $myCnt:\n";
foreach my $key (sort keys %counts) {
my $val = $counts{$key};
my @v = readCounts($val);
if ($v[$myCnt] > 0 && $v[$otherCnt] == 0) {
printf "%s\n", $key;
}
}
}
sub printCommon {
# Print common entries for both files
print "Common entries for both files:\n";
my $limit = 50;
foreach my $key (sort keys %counts) {
my $val = $counts{$key};
my @v = readCounts($val);
if ($v[0] >= $limit && $v[1] >= $limit) {
printf "%s\n", $key;
}
}
}
inputProcess('Inp1.txt', 0);
inputProcess('Inp2.txt', 1);
print "All entries and counts\n";
foreach my $key (sort keys %counts) {
my $val = $counts{$key};
my ($v0, $v1) = readCounts($val);
printf "%-16s / %3d, %3d\n", $key, $v0, $v1;
}
printUniq(0);
printUniq(1);
printCommon();
这个想法是使用(引用)数组的散列。密钥只是从文件中读取的条目。每个数组(散列中的值)包含 2 个计数器,用于两个文件。
该程序会打印已排序的条目,但如果您愿意,只需删除排序即可。
编写上面的程序,我假设每一行都是一个单独的条目。如果不是这种情况,则相应地修改inputProcess函数。
还有一点:第一个文件在每行的末尾包含Del,而第二个文件没有。这样的条目(整行)不会“匹配”。
推荐阅读
- javascript - 带有 post 参数的同步请求不起作用
- mysql - 将带有左连接和内连接的查询转换为 Knex
- javascript - 对模糊的操作,除非通过反应单击特定元素或如何获取单击元素 onBlur
- javascript - 表格按 JavaScript 中的总列排序
- angular - 输入字符串 | null 不能分配给类型字符串?如何在 Angular 应用程序中解决这个问题?
- python - 无法从包含表格数据 (iframe) 的网站解析元素
- sql - 根据 periodtype 获取期间的开始日期
- java - 如何从java中的doc模板生成动态word文档?
- c# - 在 Linux 上使用 WinSCP .NET 程序集
- php - 使用 PHP 函数比较 MySQL 数据库中的数据