-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmergepy.pl
executable file
·95 lines (77 loc) · 2.05 KB
/
mergepy.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/usr/bin/perl -w
# mergepy.pl --- Merge two eim-py table
# Last modify Time-stamp: <Ye Wenbin 2007-07-12 20:45:38>
# Version: v 0.0 2007/07/12 11:39:17
# Author: Ye Wenbin <wenbinye@163.com>
use strict;
use warnings;
use Pod::Usage;
use Getopt::Long qw(:config no_ignore_case auto_help);
my ($outfile);
GetOptions(
'output=s' => \$outfile,
);
my ($prime_table, $added_table) = @ARGV;
my $max_index = 10000;
my %table;
my $outfh = \*STDOUT;
if ( $outfile ) {
open(OUT, ">", $outfile) or die "Can't open file $outfile: $!";
$outfh = \*OUT;
}
open(FH1, $prime_table) or die "Can't open file $prime_table: $!";
open(FH2, $added_table) or die "Can't open file $added_table: $!";
# read prime table
while ( <FH1> ) {
print $outfh $_;
last if /^\[Table\]$/;
}
while ( <FH1> ) {
next if /^\s*$/;
next if /^[^a-z]/; # not right format
chomp;
my @r = split /\s+/;
$table{$r[0]} = {
map { $r[$_] => $_ } 1..$#r
};
}
close FH1;
# add all items in $add_table to the end of the prime table
while ( <FH2> ) {
last if /^\[Table\]$/;
}
while ( <FH2> ) {
next if /^$/;
next if /^[^a-z]/; # not right format
chomp;
my @r = split /\s+/;
my $item = $table{$r[0]};
if ( defined $item ) {
foreach ( 1..$#r ) {
next if exists $item->{$r[$_]};
$item->{$r[$_]} = $max_index + $_;
}
} else {
$table{$r[0]} = {
map { $r[$_] => $_ } 1..$#r
};
}
}
close FH2;
foreach ( sort keys %table ) {
my $item = $table{$_};
print $outfh join(' ', $_, sort {$item->{$a} <=> $item->{$b}} keys %$item), "\n";
}
if ( $outfile ) {
close $outfh;
}
__END__
=head1 NAME
mergepy.pl - A utility to merge two eim-py table file
=head1 SYNOPSIS
mergepy.pl prime-table added-table [-o new_table]
Add all items in added-table to the prime-table. The duplicate items
will ignore and new items in added-table will append to prime-table
with the same order.
You may use this utility to update or enlarge your eim-py table file
without lose your own table.