因此,我试图找出如何替换特定列中的数字并将该数字替换为字母的方法。我在Perl中编码
input.txt
10004226549870
204226549870062001186000000000040008060802
5032000067470318021604226549870062001186
603200100001312293000522105000000456000131289
603200200006545553000522109000004242000654555
603200300002463923000522090000005571000246392
603200400002635413000521196000248920000263541
60320050000175960300052196600000101700017596
603200600001054853004867190000001003000105485
603200700001451223000522095000003981000145122
75030000674703180216000700017222840007
89999990674703180216000070001722284
9000013
替换表
1 -> A
2 -> B
3 -> C
4 -> D
5 -> E
6 -> F
7 -> G
8 -> H
9 -> I
0 -> {
替换应在以下情况下发生:
Line starts with a "6", for the numbers located in column 17 and column 45.
Line starts with a "7", for the number located in column 34.
Line starts with a "8", for the number located in column 35.
使用上述替换规则,生成的文件(将当前文件用作 一个示例),将导致:
output.txt
10004226549870
2042265498700620
5032000067470318021604226549870062001186
6032001000013122I3000522105000000456000131289
6032002000065455E3000522109000004242000654555
6032003000024639B3000522090000005571000246392
6032004000026354A3000521196000248920000263541
6032005000017596{300052196600000101700017596
6032006000010548E3004867190000001003000105485
6032007000014512B3000522095000003981000145122
750300006747031802160007000172228D0007
8999999067470318021600007000172228D
9000013
** MyCode
my $fn = 'input.txt';
my $wr = 'output.txt';
my %repl = (
1 => "A"
2 => "B"
3 => "C"
4 => "D"
5 => "E"
6 => "F"
7 => "G"
8 => "H"
9 => "I"
0 => "{"
);
open ( my $fh, '<', $fn ) or die "Could not open file '$fn': $!";
open ( my $ww, '>', $wr ) or die "Could not open file '$fn': $!";
my @lines;
while (my $line = <$fh>) {
chomp $line;
push @lines, $line;
if ( $line =~ /^6/) {
foreach my $key (sort keys %repl){
substr($line, 17, 1) =~ s/\b$key\b/$repl{$key}/g
substr($line, 45, 1) =~ s/\b$key\b/$repl{$key}/g
}
}
elsif ($line =~ /^7/) {
foreach my $key (sort keys %repl){
substr($line, 34, 1) =~ s/\b$key\b/$repl{$key}/g
}
}
elsif ($line =~ /^8/) {
foreach my $key (sort keys %repl){
substr($line, 35, 1) =~ s/\b$key\b/$repl{$key}/g
}
}
else {
print $ww $_;
}
}
close $fh;
close $ww;
答案 0 :(得分:4)
这很好,我能看到的唯一问题是您可能计数错了,因为substr的计数从0开始。另一件事是,每一行都需要写入输出文件,因此丢失最后else
,然后写出该行,无论是否更改。†
但是,我会提供改进。不需要通过为每个键运行一个正则表达式来探查字符,这既浪费又昂贵。还有其他方法,更简单有效。
使用substr
,我们需要在动态确定替换位置时进行两次通话
substr $line, 16, 1, $repl{ substr $line, 16, 1 };
这将用位置1
中的值替换键17处长度为%repl
的子字符串,该键是位置17中长度为1
的子字符串。
但是,如果字符串短于所需位置,则此行可能会出现问题。实际上,这是用{
作为替换行的情况,该行只有44个字符。考虑到substr
具有特定情况的“特定”行为,最好不要进行“修复”,而应进行检查
# for $col == 45 (etc)
if (length $line >= $col) {
substr $line, $col-1, 1, $repl{ substr $line, $col-1, 1 };
}
可以将这些位置准备为数组,以避免分散的幻数或标量。
另一种方法是使用正则表达式
$line =~ s/.{16}\K(.)/$repl{$1}/;
这会匹配16个字符,\K
会删除这些匹配项,因此不需要放回它们,然后捕获下一个字符,然后将其替换为哈希中的值。现在可能不存在的位置(字符串的结尾)不需要特殊处理,因为匹配只会失败。
可以使用以上两种方法中的任何一种来代替foreach
键上的%repl
循环,并且您的代码应该按其原样工作,除了需要删除的else
分支之外和$line
刚刚打印。我也希望您有
use warnings;
use strict;
在开始的某个地方,因为每个程序都强烈建议使用这些符号。
在6
/ 7
/ 8
上匹配一行是可以的,但是您可以使用这些数字作为键来构建哈希,并且它们的值是执行所需替换的代码引用。如果这些数字经常变化(或者可能会有更多变化),请考虑做类似的事情。
†最后,由于一旦在print $ww $_;
条件下使用了主题分析器,{{1} }),则不再提供else
(它是未定义的)。
但是,这可能是一个发布问题,因为它在定义的句柄为while
时使用$line
,并且还有一些其他错字。
答案 1 :(得分:2)
我通常喜欢避免使用正则表达式引擎,除非我匹配模式。这是我可能的方法。
use strict;
use warnings;
my %table = (
1 => 'A', 2 => 'B', 3 => 'C', 4 => 'D', 5 => 'E',
6 => 'F', 7 => 'G', 8 => 'H', 9 => 'I', 0 => '{',
);
my %subs = (
6 => [17, 45],
7 => [34],
8 => [35],
);
while (my $line = <DATA>) {
chomp $line;
my $cols = $subs{ substr($line, 0, 1) } // [];
for my $col (@$cols) {
next if length($line) < $col;
my $char = substr($line, $col - 1, 1);
substr($line, $col - 1, 1) = $table{$char};
}
print "$line\n";
}
__DATA__
10004226549870
204226549870062001186000000000040008060802
5032000067470318021604226549870062001186
603200100001312293000522105000000456000131289
603200200006545553000522109000004242000654555
603200300002463923000522090000005571000246392
603200400002635413000521196000248920000263541
60320050000175960300052196600000101700017596
603200600001054853004867190000001003000105485
603200700001451223000522095000003981000145122
75030000674703180216000700017222840007
89999990674703180216000070001722284
9000013
答案 2 :(得分:1)
用字符定义数组@repl
,创建哈希%match
,使用%match
哈希键作为输入正则表达式过滤器。
读取数据,如果行与正则表达式过滤器匹配,则将其传递以进行进一步处理。正则表达式捕获匹配数字,我们稍后将其用于从%match
哈希中提取位置。
将输入行拆分为一个数字数组,并将感兴趣的位置的数字替换为@repl
数组中的数字。
将行与join
组合在一起并打印结果。
use strict;
use warnings;
use feature 'say';
my @repl = split '', '{ABCDEFGHI';
my %match = ( 6 => [17,45], 7 => [34], 8 => [35] );
my $filter = join('|',keys %match);
my $re = qr/^($filter)/;
while(my $line = <DATA>) {
chomp $line;
if( $line =~ /$re/ ) {
my @nums = split '', $line;
for my $pos ( @{$match{$1}} ) {
my $i = $pos-1;
$nums[$i] = $repl[$nums[$i]] if defined $nums[$i];
}
$line = join '', @nums;
}
say $line;
}
__DATA__
10004226549870
204226549870062001186000000000040008060802
5032000067470318021604226549870062001186
603200100001312293000522105000000456000131289
603200200006545553000522109000004242000654555
603200300002463923000522090000005571000246392
603200400002635413000521196000248920000263541
60320050000175960300052196600000101700017596
603200600001054853004867190000001003000105485
603200700001451223000522095000003981000145122
75030000674703180216000700017222840007
89999990674703180216000070001722284
9000013
输出
10004226549870
204226549870062001186000000000040008060802
5032000067470318021604226549870062001186
6032001000013122I300052210500000045600013128I
6032002000065455E300052210900000424200065455E
6032003000024639B300052209000000557100024639B
6032004000026354A300052119600024892000026354A
6032005000017596{300052196600000101700017596
6032006000010548E300486719000000100300010548E
6032007000014512B300052209500000398100014512B
750300006747031802160007000172228D0007
8999999067470318021600007000172228D
9000013