如何使用“s ///”运算符来更改值的数量?

时间:2022-04-19 22:55:59

I have a file that contains:

我有一个包含以下内容的文件:

foo1 = 1

foo2 =  2

foo3 =    8

.
.
.

I need to replace only the values (1,2,8...) in that file with part of a memory hash values, the ones with the same keys (foo1 -> 33,foo2 -> 44,foo3...) How can I change it using the "s///" operator? If there is other elegant ways to conduct it I'll be happy to know.

我需要用一部分内存哈希值替换该文件中的值(1,2,8 ...),具有相同键的值(foo1 - > 33,foo2 - > 44,foo3 ......)如何使用“s ///”运算符更改它?如果有其他优雅的方式来进行,我会很高兴知道。

Thanks for any help,

谢谢你的帮助,

Yohad.

4 个解决方案

#1


one way

%hash = ("foo1"=>33,"foo2" => 44,"foo3"=>99);
while (<>){
  chomp;
  ( $one , $two ) = split /\s+=\s+/, $_;
  print "$one = $hash{$one} \n"
}

#2


my %new_values = ( foo1 => 33, ... );
$data =~ s{^(?<key>\w+) = \K(?<old_value>.+)$}
          {$new_values{$+{key}}}gem;

The key is the "e" flag which lets you run code to determine the replacement. The (?<...>) syntax increases readability, and the \K allows us to match the entire line but only replace the value area. The "g" flag repeats the substitution as many times as possible, and the "m" flag makes ^...$ match a line instead of the entire string. (The g and m will probably be unnecessary if you split the lines before application of the regexp.)

关键是“e”标志,它允许您运行代码以确定替换。 (?<...>)语法提高了可读性,而\ K允许我们匹配整行但只替换值区域。 “g”标志尽可能多地重复替换,“m”标志使^ ... $匹配一行而不是整个字符串。 (如果在应用正则表达式之前拆分行,则g和m可能是不必要的。)

#3


Here's one

%h = ("foo1"=>3, "foo2"=>5);
while (<>)
{
    #Substitute value according to expression on right hand side
    s/(\w+) = .*/$1 . " = ". $h{$1}/e;
    print;
}

#4


s/regexPattern/replacementPattern/flags

"I am a string!"

“我是一根绳子!”

s/\sam/'s/g

"I's a string!"

“我是一根绳子!”

http://gnosis.cx/publish/programming/regular_expressions.html

I really can't understand what you're doing based on the descriptoin. Can you provide sample input and output?

根据描述,我真的无法理解你在做什么。你能提供样品输入和输出吗?

#1


one way

%hash = ("foo1"=>33,"foo2" => 44,"foo3"=>99);
while (<>){
  chomp;
  ( $one , $two ) = split /\s+=\s+/, $_;
  print "$one = $hash{$one} \n"
}

#2


my %new_values = ( foo1 => 33, ... );
$data =~ s{^(?<key>\w+) = \K(?<old_value>.+)$}
          {$new_values{$+{key}}}gem;

The key is the "e" flag which lets you run code to determine the replacement. The (?<...>) syntax increases readability, and the \K allows us to match the entire line but only replace the value area. The "g" flag repeats the substitution as many times as possible, and the "m" flag makes ^...$ match a line instead of the entire string. (The g and m will probably be unnecessary if you split the lines before application of the regexp.)

关键是“e”标志,它允许您运行代码以确定替换。 (?<...>)语法提高了可读性,而\ K允许我们匹配整行但只替换值区域。 “g”标志尽可能多地重复替换,“m”标志使^ ... $匹配一行而不是整个字符串。 (如果在应用正则表达式之前拆分行,则g和m可能是不必要的。)

#3


Here's one

%h = ("foo1"=>3, "foo2"=>5);
while (<>)
{
    #Substitute value according to expression on right hand side
    s/(\w+) = .*/$1 . " = ". $h{$1}/e;
    print;
}

#4


s/regexPattern/replacementPattern/flags

"I am a string!"

“我是一根绳子!”

s/\sam/'s/g

"I's a string!"

“我是一根绳子!”

http://gnosis.cx/publish/programming/regular_expressions.html

I really can't understand what you're doing based on the descriptoin. Can you provide sample input and output?

根据描述,我真的无法理解你在做什么。你能提供样品输入和输出吗?