Re: [問題] 檔案字串比對
列出聯集交集的版本。
============================code====================================
#!/usr/bin/env perl
use strict;
open my $filehandle, "testfile.txt";
my %records;
while (<$filehandle>) {
my @cols = split /\s+/;
$records{$cols[0]} = [split /,/, $cols[2]];
$records{$cols[1]} = [split /,/, $cols[3]];
}
close $filehandle;
generate_data(%records);
sub generate_data {
my %records = @_;
my @names = sort keys %records;
printf "%8s\t%8s\t%30s\t%20s\n", "Record A", "Record B", "Union",
"Intersection";
for my $first (0..$#names-1) {
for my $second ($first+1..$#names) {
my %count;
my $firstname = $names[$first];
my $secondname = $names[$second];
for (@{$records{$firstname}}, @{$records{$secondname}}) {
$count{$_}++;
}
printf "%8s\t%8s\t%30s\t%20s\n",
$firstname, $secondname,
"[" . (join ",", keys %count) . "]", # union
"[" . (join ",", grep { $count{$_} > 1 } keys %count) . "]";
#intersection
}
}
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.214.6
推
06/09 22:13, , 1F
06/09 22:13, 1F
討論串 (同標題文章)
Perl 近期熱門文章
PTT數位生活區 即時熱門文章
10
113