看板 Perl 關於我們 聯絡資訊
列出聯集交集的版本。 ============================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
cp3cp3:多謝! 06/09 22:13