in reply to undef a variable for a loop

Given the repost, a new thread seemed appropriate. There are a number of changes I would suggest, listed below:
  1. 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.
  2. 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).
  3. 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.
  4. 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.
  5. Foreach Loops are generally considered preferable to For Loops since they require less typing and are less prone to bugs.
  6. 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.
  7. Given how you were handling your outputs, it's dramatically easier to have 1 array vs. 5 scalars.
  8. 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?
  9. A regular expression in list context returns the capture values from the expression, allowing you to avoid using the variables $1, $2, ...
  10. 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;

Replies are listed 'Best First'.
Re^2: undef a variable for a loop
by csiepka (Initiate) on May 18, 2009 at 19:26 UTC
    I really hope that one day I can write scripts like you just did. I'm still learning - something new every day. Thing is I love it and I hope to master it. I won't take your suggestions lightly and I appreciate the time you took to help me out. Thank you very much Kennethk! :)