I'm a newcomer to programming and have within the last few months been learning to use Perl. I've searched your archives and have honestly tried to figure this out on my own, but after a few days of banging my head I've given up.
I'm searching the current directory for files with a .RPT extension, then storing the names in an array. I then want to open each file as well as write to an OUTPUT file (with the same name but a .TXT extension.) While I have the INPUT file open I want to search on a pattern and write the matching pattern to the OUTPUT file. The expressions I want to match are stored in an array
params, which is generated from the file params.txt in the current directory.
The code is below.
#!/apps/bin/perl
use warnings;
use strict;
#search directory for files ending with .RPT and store the names in an
+ array
my @files=glob("*.RPT");
my $i; my $x;
#for each file in array open and search for string
for $i (@files){
open INPUT, "$i" or die "Couldn't open the file. $!";
open OUTPUT, "> $i\.dat";
print OUTPUT "$i\n";
print OUTPUT "\n Parameter Value\n";
print OUTPUT "-------------------------\n";
while (<INPUT>){
#open file containing expressions to search for
open FILE, "params.txt" or die "Couldn't open the file. $!";
my @params=<FILE>;
for $x (@params) {
if (/\s$x\s*\d*/){
$x=$&;
print OUTPUT "$x\n";
}
}
}
}
I'd really appreciate any help you can offer.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.