Given the repost, a new thread seemed appropriate. There are a number of changes I would suggest, listed below:
- You should avoid just creating a series of script globals at the start of a file. Since my lets you create scoped variables, take advantage of that and keep your variables scoped. Proper scoping would have solved your problem, since the output variables would be set to undef on every iteration of the loop.
- If you do have script parameters, you should collect them at the top of the file. I found three values I thought that applied to (list.txt, the @exch list and $dircon).
- By using a lexical file handle instead of a bareword, you allow the same kind of typographical protections on your file/directory handles you get with you other variables.
- You should should use the three-argument form of open, especially when taking outside input for your file names. A maliciously named file in your "contents" directory would allow execution of arbitrary code.
- Foreach Loops are generally considered preferable to For Loops since they require less typing and are less prone to bugs.
- If you are going to read an entire file, angle brackets (<>) in list context return the entire contents of the file, split on the input record separator $/. As well, chomp acts on lists.
- Given how you were handling your outputs, it's dramatically easier to have 1 array vs. 5 scalars.
- Since you only want the last occurrence of your variables, why not start at the end of your file, and break with a last on the first hit?
- A regular expression in list context returns the capture values from the expression, allowing you to avoid using the variables $1, $2, ...
- A curly bracket closing a block does not require a semicolon.
For you consideration, I'm implemented all of these comments into your newly posted code:
#!/usr/bin/perl
use warnings;
use strict;
my $dircom = "completes";
my $list_file = "list.txt";
my @exch = qw(EXCH1 EXCH2 EXCH3 EXCH4 EXCH5);
opendir my($mydir), $dircom;
my @files = grep !/^\.\.?$/, readdir $mydir;
close ($mydir);
open (my $lis, "+>", $list_file) || die ("Can't open $list_file");
foreach my $file (@files) {
open (my $fil, "$dircom\\$file") || die ("test");
my @completefile = <$fil>;
chomp @completefile;
close $fil;
print $lis "\n$file\n";
foreach my $exch (@exch) {
my @su_output = ();
my @dc_output = ();
foreach (reverse @completefile) {
@su_output = /^.+\\(.+)_.+\..+?:(.+?)\s-\s(.+?)\s.+\/($exc
+h)\s\|.+::(.*Up)\(\)/;
last if @su_output;
}
foreach (reverse @completefile) {
@dc_output = /^.+\\(.+)_.+\..+?:(.+?)\s-\s(.+?)\s.+\/($exc
+h)\s\|.+::(.*Up)\(\)/;
last if @dc_output;
}
if(@su_output) {
print $lis join(",",@su_output), "\n";
} else {
print $lis "No su results for $exch";
}
if(@dc_output) {
print $lis join(",",@dc_output), "\n";
} else {
print $lis "No dc results for $exch";
}
}
}
close $lis;
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.