Presumably the params.txt file doesn't change while you're iterating over the *.RPT files, so you should take that bit out of the loop.

You also say

I then want to open each file as well as write to an OUTPUT file (with the same name but a .TXT extension.)
but in your code, you give the output file a .dat suffix. Oh, and you aren't checking the return value of your open for the OUTPUT filehandle.

Make sure that if your @params array contains items with characters that are special in regular expressions that you mean for them to be used as such.

Are you sure that your regular expression for the each @params is correct? You want to match exactly one whitespace character, followed by a single element of the @params array, optional whitespace, then optional numbers? Judging from the heading in your output, you should probably capture the parameter and those optional digits separately. Here's an untested rewrite:

#!/usr/bin/perl use warnings; use strict; open PARAMS, "params.txt" or die "Couldn't open the file. $!"; my @params=<PARAMS>; close PARAMS; my @files = glob("*.RPT"); for my $i (@files) { open INPUT, "$i" or die "Couldn't open the file. $!"; open OUTPUT, ">$i.dat" or die "Couldn't write to $i.dat - $!"; print OUTPUT "$i\n\n"; print OUTPUT " Parameter Value\n"; print OUTPUT "-------------------------\n"; while (<INPUT>) { for my $x (@params) { next unless /\s($x)\s*(\d*)/; print OUTPUT "$1 $2\n"; # probably use printf() here } } close INPUT; close OUTPUT; }

If the things in @params are just words (i.e., no regular expression special characters), you probably want to make a single regular expression out of them ahead of time and rewrite the inner loop like so:

... my $paramre = join '|', @params; ... while (<INPUT>) { next unless /\s($paramre)\s*(\d*)/; print OUTPUT "$1 $2\n"; } ...

Of course, even if they contain regular expression special characters you might want to do this, but be really careful about it. :-)


In reply to Re: Using expressions in arrays for pattern matching by duff
in thread Using expressions in arrays for pattern matching by Mike_76

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.