Created
July 13, 2017 03:01
-
-
Save ywindish/914e497418a11adfd57398a9fae9cd2f to your computer and use it in GitHub Desktop.
2ファイルのうちお互いにしか無い行を抽出する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
# 2ファイルのうちお互いにしか無い行を抽出します。 | |
# | |
use strict; | |
use warnings; | |
use utf8; | |
use feature 'say'; | |
my $file_left = './left.txt'; | |
my $file_right = './right.txt'; | |
my %data = {}; | |
open my $fh1, '<', $file_left, or die $!; | |
while (<$fh1>) { | |
chomp; | |
$data{$_} = {left => 1, right => 0 }; | |
} | |
close $fh1; | |
open my $fh2, '<', $file_right, or die $!; | |
while (<$fh2>) { | |
chomp; | |
my $item = $data{$_}; | |
if ($item) { | |
$item->{right} = 1; | |
} else { | |
$item = { left => 0, right => 1 }; | |
} | |
$data{$_} = $item; | |
} | |
close $fh2; | |
# TODO 2ファイルに出し分ける | |
for my $key (sort keys %data) { | |
next if $data{$key}->{left} && $data{$key}->{right}; | |
say $key, ',left' if $data{$key}->{left}; | |
say $key, ',right' if $data{$key}->{right}; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment