Re: [問題] 檔案字串比對

看板Perl作者 (St. Kevin)時間15年前 (2010/06/07 17:13), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串4/4 (看更多)
列出聯集交集的版本。 ============================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
文章代碼(AID): #1C3BWo2v (Perl)
文章代碼(AID): #1C3BWo2v (Perl)