【发布时间】:2018-06-28 22:32:01
【问题描述】:
重新运行.txt
a,1
b,2
c,3
d,4
(在我的代码中,a、b、c、d 分别是 $var 和 1、2...$num)
我想在cell.txt 中搜索$var 并在此文件中将area(对应的下一行)替换为$num(如area : 1)
cell.txt
cell (a) {
area : 2
}
cell (b) {
area : 2.3
}
cell (c) {
area : 2.5
}
cell (d) {
area : 2.7
}
Perl 代码
#!usr/bin/perl
use warnings;
use strict;
open( my $fh1, "rerun.txt" ) or die "Couldn't open file file.txt, $!";
my $word = 0;
my $input = "area";
my $num;
my $var;
my $line;
my $a = 0;
my $flag = 0;
my $flag1 = 0;
while ( <$fh1> ) {
( $var, $num ) = split ","; # splitting acc to comma
open( my $fh, "cell.txt" ) or die "Couldn't open file file.txt, $!";
while ( my $line1 = <$fh> ) { # while in the file opened
$line1 =~ s/^\s+//; # removing spaces
my @word = split " ", $line1; # splitting acc to spcaes
foreach $word ( @word ) {
$word =~ s/[(,),]//g; # excluding all brackets (,),{,}
if ( $word eq $var ) {
$flag = 1;
}
if ( $flag == 1 ) {
if ( $word eq "area" ) {
$a = $.; # saving the line number
system( "sed -i '$a s/.*/\t area : $num /' cell.txt" );
goto L1;
}
}
}
}
L1:
close( $fh );
}
close( $fh1 );
【问题讨论】:
-
TCL跟它有什么关系?
-
我已经整理了你的 Perl 代码,以便我可以阅读它,但我需要这样做是可耻的。下次在这里提问时请多多努力。
-
cell.txt 中的每一项都会在 rerun.txt 中吗?
-
@trenton-trama: cell.txt 中需要更改的变量很少,不是全部。