in reply to undef a variable for a loop

I don't see why you have any problems. Cleaned up:

for my $exch (@exch) { for my $i (0 .. $#completefile) { if (my ($su_adapter, $su_date) = $completefile[$i] =~ /.../) { print $su_adapter, $su_date; } } }

Or is the print outside of the if? That wouldn't make any sense.

Replies are listed 'Best First'.
Re^2: undef a variable for a loop
by csiepka (Initiate) on May 15, 2009 at 18:41 UTC
    Actually the print IS outside of the if, and that's only because all I want is the final match for the $exch, etc of each file.
      That's what it does already thanks to the leading /.+/. Well, you might have to replace the leading /.+/ with /(?s:.)+/ so that it matches newlines.
      Based on this additional information, you probably mean something like this:

      foreach $exch(@exch) { undef($su_adapter); undef($su_date); for($i=0; $i<=$#completefile; $i++) { if($completefile[$i]=~ m/^.+\\(.+)_.+\..+?:(.+?)\s-\s(.+?)\s.+ +?\/$exch\s\|.+::(.*Up)\(\)/) { $su_adapter=$1; $su_date=$2; } } print $su_adapter, $su_date; }

      Note that there are a bunch of easy modifications (as shown in ikegami's post) that can simplify your code and reduce its predilection for bugs.

        No. Four problems with your solution:
        • undef($var); is less efficient than $var = undef;.
        • The print still needs to be in an if in case there was no match.
        • Using undef at all is useless (because of previous point).
        • That doesn't find the last match of each file. It only finds the last match of one file.