use strict;
use warnings;
#store params from file1
my (%params);
open(my $FILE1, "< file1.txt") or die $!;
my @arr = <$FILE1>;
foreach(@arr){
#extract key-value pairs \s* = any number of spaces, .* = anything
#\w+ = any sequence of letters (at least one)
my ($key, $value) = ($_ =~ /(\w+)\s*:\s*(.*)/);
$params{$key}=$value;
}
close $FILE1;
open(my $FILE2, "< file2.txt") or die $!;
my @arr2 = <$FILE2>;
foreach(@arr2){
my ($key, $value) = ($_ =~ /(\w+)\s*:\s*(.*)/);
#if the found key did exist in params, then replace the found value, with
#the value from file 1 (this may be dangerous if value contains regexp chars,
#consider using e.g. \Q...\E
if (exists $params{$key}) {
#replace the row in @arr2 inline using direct ref $_
$_ =~ s/$value/$params{$key}/;
}
}
close $FILE2
#replace/output the result /here to a different file, not to destroy
open(my $FILE2W, "> file2_adjusted.txt") or die $!;
print $FILE2W @arr2;
close $FILE2W
這聽起來像功課(更換就地並保持參數順序是在小KVP配置文件中具有零實際壽命相關性的任意約束)。你能告訴我你嘗試過什麼,遇到了什麼困難? StackOverflow不是「爲我做的工作/家庭作業」網站。 – DVK