【问题标题】:Reading & modifying data from lib file using perl使用 perl 从 lib 文件中读取和修改数据
【发布时间】:2017-01-30 10:52:59
【问题描述】:

我必须从 .lib 文件中读取和修改某些数据。 我可以提取数据所在的行,但无法提取与其关联的数字。

open my $fh, "<" ,".....lib" or die "$!";

while(my $line = <$fh>) {
      if ($line =~ m/xyz0/) {
         print $line;
      }
} 

代码输出行:

+xyz0 = 0.005 hg = 0.9 rvfd = 75

我想从中提取 xyz0 旁边的值。我该怎么做?

【问题讨论】:

    标签: regex perl


    【解决方案1】:
    if ($line =~ m/xyz0 = (\S+)/){
        print $1;
    }
    

    【讨论】:

    • 或者如果你想避免全局变量:if ( my ($xyz0) = $line =~ m/xyz0 = (\S+)/) { print $xyz0, "\n"; }
    • @rahdirs 如果您不确定= 周围的空格,请使用/xyz0\s*=\s*(\S+)/
    • 如何将提取的值更改为某个新数字?像 0.9 ?
    • @rahdirs 一种方法是使用substitution operator
    • @zdim,@ysth,@ikegami: while(my $line = &lt;$fh&gt;){ if( my ($xyz0) = $line =~m/xyz0 = (\S+)/){ print $xyz0,"\n"; # this prints 0.005 $new_value = 0.9; $xyz0 =~ s/$xyz0/$new_value/; # How do I enter this value to file ?}}.
    猜你喜欢
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 2015-04-29
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 2012-09-06
    相关资源
    最近更新 更多