具体代码如下:
#行内词头处理
$contents =~ s/^(.+行内词头.+)$/ci_hang_nei($1)/meg;
sub ci_hang_nei
{
my $arg = shift;
my $c_number;
#获得行内词头所在行数
#https://stackoverflow.com/questions/4918244
open my $handle, '<', "./词头.txt" or die $!;
while (<$handle>) {
$c_number = $., last if /$arg/;
}
close $handle;
#行数递减,获得行内词头归属
my $c_get;
my $c_get_page;
my $c_get_cn;
my $c_get_en;
my $c_get_da;
my $c_get_xiao;
until(0){
$c_number--;
$c_get=qx(sed -n '$c_number p' ./词头.txt);
if($c_get =~ m/行内词头/ ){
redo;
}else{
chomp($c_get); #去除未尾换行符
$c_get_page=$c_get;
$c_get_page =~ s/^([0-9]+) \t.+/${1}/m;
$c_get_cn=$c_get;
$c_get_cn =~ s/^.+\t(.+)\t.+\t.+\t.+/${1}/m;
$c_get_cn =~ s/(.+?)&.+/${1}/m;
$c_get_en=$c_get;
$c_get_en =~ s/^.+\t.+\t(.+)\t.+\t.+/${1}/m;
$c_get_en =~ s/(.+?)&.+/${1}/m;
$c_get_da=$c_get;
$c_get_da =~ s/^.+\t.+\t.+\t(.+)\t.+/${1}/m;
$c_get_xiao=$c_get;
$c_get_xiao =~ s/^.+\t.+\t.+\t.+\t(.+)/${1}/m;
last;
}
}
#行内词头信息拼接
$arg=$arg.$c_get_page." & ".$c_get_cn." & ".$c_get_en." & ".$c_get_da." & ".$c_get_xiao;
return $arg;
}