#!/usr/local/bin/perl

open (CANTON, "Canton.data.bak");
open (DATA, ">Canton.data.new");
$prev = aa1;
while (<CANTON>) {
	/^([^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*)\t(\S*)\n$/;
	$field = $1;
	$pho = $2;
	if ($pho =~ /^$prev\d$/) {
		&add_on;
		$pho =~ /^(\D+)/;
		$prev = $1;
	} else {
		print DATA @t1, @t2, @t3, @t4, @t5, @t6;
		@t1 = @t2 = @t3 = @t4 = @t5 = @t6 = "";
		&add_on;
		$pho =~ /^(\D+)/;
		$prev = $1;
	}
}
print DATA @t1, @t2, @t3, @t4, @t5, @t6;
close (CANTON);
close (DATA);

sub add_on {
	if ($pho =~ /1$/) { push(@t1, "$field\t$pho\n");}
	elsif ($pho =~ /2$/) {push(@t1, "$field\t$pho\n");}
	elsif ($pho =~ /3$/) {push(@t2, "$field\t$pho\n");}
	elsif ($pho =~ /4$/) {push(@t3, "$field\t$pho\n");}
	elsif ($pho =~ /5$/) {push(@t4, "$field\t$pho\n");}
	elsif ($pho =~ /6$/) {push(@t5, "$field\t$pho\n");}
}
