Skip to content

Instantly share code, notes, and snippets.

@olegwtf
Created September 7, 2011 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olegwtf/1199818 to your computer and use it in GitHub Desktop.
Save olegwtf/1199818 to your computer and use it in GitHub Desktop.
missing right curly or square bracket
use autodie;
use Fcntl qw(SEEK_SET);
use strict;
use warnings;
my ($in1, $in2, $ot) = @ARGV;
die "usage: $0 input1 input2 output"
unless defined $in1 && defined $in2 && defined $ot;
open IN1, '<:urf8', $in1;
open IN2, '<:utf8', $in2;
open OT, '>:utf8', $ot;
my $in1_records = records_count(\*IN1);
my $in2_records = records_count(\*IN2);
print OT "$in1 has $in1_records, $in2 has $in2_records\n";
my ($fh_big, $fh_small) = ($in1_records > $in2_records ? (\*IN1, \*IN2) : (\*IN2, \*IN1));
my ($dict_small, $dict_big) = (make_dict($fh_small), make_dict($fh_big));
close IN1;
close IN2;
while (my ($k, $v) = each(%$dict_small)) {
# checkis record exists
unless (exists($dict_big->{$k})) {
print OT "[NOT EXISTS] '$k'\n";
next;
}
# check phones
my $record = $dict_big->{$k};
my @bad_phones;
for my $i (3..8) {
unless ($v->[$i] eq $record->[$i]) {
push @bad_phones, "$v->[$i] != $record->[$i]";
}
}
if (@bad_phones) {
print OT "[BAD PHONES] '$k'\n";
print OT "\t$_\n" for @bad_phones;
}
}
close OT;
sub records_count {
my $fh = shift;
my $cnt = 0;
$cnt++ while <$fh>;
seek $fh, 0, SEEK_SET;
return $cnt;
}
sub make_dict {
my $fh = shift;
my %dict;
while (my $str = <$fh>) {
my @fields = map { s/^["\s]+//; s/["\s]+$//; $_ } split "\t", $str;
$dict{$fields[1]} = \@fields;
}
seek $fh, 0, SEEK_SET;
return \%dict;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment