打开数组中每个元素的文件,并检查正则表达式Perl

时间:2022-10-06 14:20:32

I have an array filled with 4 digit numbers (@nums) that correspond to conf files which use the numbers as the file name, like so: 0000.conf

我有一个数组填充4位数字(@nums),对应于使用数字作为文件名的conf文件,如下所示:0000.conf

I am reading a file foreach element in the array and checking the file for a pattern like this :

我正在读取数组中的文件foreach元素并检查文件中是否有这样的模式:

use strict;
use warnings;

foreach my $num (@nums) {
    open my $fh, "<", "$num.conf"
      or warn "cannot open $num.conf : $!";
    while(<$fh>) {
        if (/^SomePattern=(.+)/) {
            print "$num : $1\n";
        }
    }
}

I am extracting the part of the pattern I want using () and the special var $1.

我正在使用()和特殊的var $ 1提取我想要的模式部分。

This seems to be working except it only prints the results of the last file that is opened, instead of printing the results each time the foreach loop passes and opens a file, which is what I expected.

这似乎是有效的,除了它只显示打开的最后一个文件的结果,而不是每次foreach循环传递并打开文件时打印结果,这是我所期望的。

I am still learning Perl, so any detailed explanations of what I missing here will be greatly appreciated.

我还在学习Perl,所以对我在这里遗漏的内容的任何详细解释都将不胜感激。

3 个解决方案

#1


1  

use v5.16;
use strict;
use warnings;

my @nums = qw/ 0000 0200 /;

for my $num (@nums){
    open my $fh, "<", "$num.conf" or die;
    while (<$fh>) {
        chomp;
        if( /^somePattern=(.+)/ ) {
            say "$1";
        }
    }
    close $fh;
}

this seems to be working for me..You are missing the close $fh; in your code, maybe that is wrong. Secondly, maybe only one of your files matches you regex, check the content for typos. I myself don't use foreach, maybe you are missing 'my' before $num. Depending of your regex, it might be useful to strim newline characters from the end of line with 'chomp'.

这似乎对我有用..你错过了接近$ fh;在你的代码中,也许这是错误的。其次,也许只有一个文件与你的正则表达式匹配,检查错别字的内容。我自己不使用foreach,也许你在$ num之前缺少'my'。根据你的正则表达式,使用'chomp'来修改行尾的换行符可能很有用。

#2


1  

Your code is excellent for a learner.

您的代码非常适合学习者。

The problem is that, using "$num.conf", you are trying to open files named 0.conf etc. instead of 0000.conf.

问题是,使用“$ num.conf”,您尝试打开名为0.conf等的文件而不是0000.conf。

You should also use the value of $! in your die string so that you know why the open failed.

你还应该使用$的值!在你的死亡字符串中,以便你知道为什么开放失败。

Write this instead

写下这个

my $file = sprintf '%04d.conf', $num;
open my $fh, '<', $file or die "Unable to open '$file': $!";

#3


0  

I have left my previous answer as it may be useful to someone. But I missed your opening "I have an array filled with 4 digit numbers".

我已经离开了我之前的答案,因为它可能对某人有用。但我错过了你的开场“我有一个充满4位数字的阵列”。

Doubtless you are populating your array wrongly.

毫无疑问,你错误地填充了你的阵列。

If you are reading from a file then most usually you have forgotten to chomp the newline from the end of the lines you have read.

如果您正在阅读文件,那么大多数情况下您通常忘记从您阅读的行末尾选择换行符。

You may also have non-printable characters (usually tabs or spaces) in each number.

您可能还在每个数字中包含不可打印的字符(通常是制表符或空格)。

You should use Data::Dumper or the better and more recent Data::Dump to reveal the contents of your array.

您应该使用Data :: Dumper或更好和更新的Data :: Dump来显示数组的内容。

#1


1  

use v5.16;
use strict;
use warnings;

my @nums = qw/ 0000 0200 /;

for my $num (@nums){
    open my $fh, "<", "$num.conf" or die;
    while (<$fh>) {
        chomp;
        if( /^somePattern=(.+)/ ) {
            say "$1";
        }
    }
    close $fh;
}

this seems to be working for me..You are missing the close $fh; in your code, maybe that is wrong. Secondly, maybe only one of your files matches you regex, check the content for typos. I myself don't use foreach, maybe you are missing 'my' before $num. Depending of your regex, it might be useful to strim newline characters from the end of line with 'chomp'.

这似乎对我有用..你错过了接近$ fh;在你的代码中,也许这是错误的。其次,也许只有一个文件与你的正则表达式匹配,检查错别字的内容。我自己不使用foreach,也许你在$ num之前缺少'my'。根据你的正则表达式,使用'chomp'来修改行尾的换行符可能很有用。

#2


1  

Your code is excellent for a learner.

您的代码非常适合学习者。

The problem is that, using "$num.conf", you are trying to open files named 0.conf etc. instead of 0000.conf.

问题是,使用“$ num.conf”,您尝试打开名为0.conf等的文件而不是0000.conf。

You should also use the value of $! in your die string so that you know why the open failed.

你还应该使用$的值!在你的死亡字符串中,以便你知道为什么开放失败。

Write this instead

写下这个

my $file = sprintf '%04d.conf', $num;
open my $fh, '<', $file or die "Unable to open '$file': $!";

#3


0  

I have left my previous answer as it may be useful to someone. But I missed your opening "I have an array filled with 4 digit numbers".

我已经离开了我之前的答案,因为它可能对某人有用。但我错过了你的开场“我有一个充满4位数字的阵列”。

Doubtless you are populating your array wrongly.

毫无疑问,你错误地填充了你的阵列。

If you are reading from a file then most usually you have forgotten to chomp the newline from the end of the lines you have read.

如果您正在阅读文件,那么大多数情况下您通常忘记从您阅读的行末尾选择换行符。

You may also have non-printable characters (usually tabs or spaces) in each number.

您可能还在每个数字中包含不可打印的字符(通常是制表符或空格)。

You should use Data::Dumper or the better and more recent Data::Dump to reveal the contents of your array.

您应该使用Data :: Dumper或更好和更新的Data :: Dump来显示数组的内容。