sravs448 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have an array of files. Each file with few lines of text, out of which I am trying to get few specific strings through regex in perl Update: Updated as per Choroba's suggestion.
use strict; use warnings; foreach my $myfile (@myFiles) { open my $FILE, '<', $myfile or die $!; while (my $line = <$FILE>) { my ($project, $value1, $value2) = <Reg exp> ,$line; print "Project : $1 \n"; print "Value1 : $2 \n"; print "Value2 : $3 \n"; } close (FILE); }

File Content

Checking Project foobar <few more lines of text here> Good Files excluding rules: 15 - 5% Bad Files excluding rules: 270 - 95% Good Files including rules: 15 - 5% Bad Files including rules: 272 - 95% <few more lines of text here>

Desired Output

Project :foobar Value1 : Good Files excluding rules: 15 - 5% Bad Files excluding rules: 270 - 95% Value2 : Good Files including rules: 15 - 5% Bad Files including rules: 272 - 95%

Replies are listed 'Best First'.
Re: Perl: Regular Expression
by choroba (Cardinal) on Sep 18, 2014 at 17:08 UTC
    You opened the file correctly (some could say there are safer ways, as the 3-argument form of open with a lexical filehandle), but you didn't read from it at all. You used
    my $line =~ / /

    as a loop condition, but it's never true: you create a new variable, so it's empty, so it can't match a space. Instead, do something like

    open my $FILE, '<', $myfile or die $!; while (my $line = <$FILE>) { # Read a line f +rom the file. my ($project, $value1, $value2) = split ' ', $line;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Perl: Regular Expression
by Anonymous Monk on Sep 18, 2014 at 17:05 UTC