Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello everyone, I have taken a shot at a perl script below, can anyone help me out and tell me why my $newFile variable is not getting reset, can someone help me on this
foreach $arrFileList(@arrFileList) { print "I am doing this file $arrFileList\n"; open (FILE, "$sourceDir/$arrFileList") or die "could not open $sour +ceDir/$arrFileList: $!\n"; @Lines = <FILE>; close FILE; $origField = (split /\|/, $Lines[3])[-3]; $commonField = (split /\|/, $Lines[13])[-1]; $commonField = "NX2|01|$commonField"; chomp $commonField; print "my commonField is this $commonField\n"; @ARGV = <$responseDir/*>; while (<>) { $newFile = $ARGV, last if /^"$commonField"$/; } print "my newFile to change is $newFile\n"; if (defined($newFile)) { system ("perl -pi -e 's/$replaceField/$origField/g' $newFile"); print "Done doing perl s/$replaceField/$origField/g $newFile\n"; system ("mv $newFile $responseDone"); print "Done moving $newFile to $responseDone\n"; system ("mv $sourceDir/$arrFileList $processed"); print "Done moving $sourceDir/$arrFileList to $processed\n"; } }

Replies are listed 'Best First'.
Re: Help on foreach loop
by Roy Johnson (Monsignor) on Sep 09, 2004 at 15:15 UTC
    It would seem that your regex isn't matching on subsequent passes. Since you never reset it, it won't be undef, it will be whatever it used to be.
    @ARGV = <$responseDir/*>; undef $newFile; # Force reset while (<>) { $newFile = $ARGV, print(STDERR "Found $commonField\n"), last +if /^"$commonField"$/; } print "my newFile to change is $newFile\n";

    Caution: Contents may have been coded under pressure.
      it still dosen't reset $newFile, for the first iteration $newFile = sample.txt in the next iteration $newFile is still sample.txt, and in my script I clearly move the sample.txt to another diretory after it was found the first time,
        Sounds like $ARGV isn't changing, possibly because the file isn't closed? Try closing ARGV within the while loop:
        @ARGV = <$responseDir/*>; while (<>) { $newFile = $ARGV, close ARGV, last if /^"$commonField"$/; }

        Caution: Contents may have been coded under pressure.
Re: Help on foreach loop
by Random_Walk (Prior) on Sep 09, 2004 at 15:01 UTC
    Your code..
    @ARGV = <$responseDir/*>; while (<>) { $newFile = $ARGV, last if /^"$commonField"$/; }

    Is going through each file in $responseDir opening it and reading each line. when you get a line which matches common field you will edit that file then go on to the next element of @arrFileList. Perhaps you wanted something more like

    # replace from @ARGV = <$responseDir/*>; foreach $file (<$responseDir/*>) { open FH, "$responseDir/$file" or die "$!\n"; my ($edit, @temp); while (<FH>) { push @temp, $_; $edit++ if /^"$commonField"$/; } close FH; next unless $edit; open NEW, ">$responseDone/$file" or die "$!\n"; foreach (@temp) { s/$replaceField/$origField/g; print NEW; } close NEW; print "Done s/$replaceField/$origField/g $file\n"; unlink "$responseDir/$file"; print "removed original\n"; system ("mv $sourceDir/$arrFileList $processed"); print "moved $sourceDir/$arrFileList to $processed\n"; }

    Cheers,
    R.

Re: Help on foreach loop
by ysth (Canon) on Sep 09, 2004 at 15:48 UTC
    I think @ARGV is only reaccessed by <> when the ARGV file is at eof/closed. Try: $newfile = $ARGV, close(ARGV), last if /^"$commonField"$/;
      nope .. didn't work
        I think it's still needed, even if it doesn't fix your apparent problem. Have you printed out @ARGV before and after the while loop to see if it is as you expect? I suggest lots of print statements or a run through with perl -d.
Re: Help on foreach loop
by Prior Nacre V (Hermit) on Sep 09, 2004 at 14:46 UTC
    ... why my $newFile variable ...

    It actually does need to be:

    my $newFile ...

    Also, your while block should include the print and if block.

    Regards,

    PN5

      didn't work, maybe my syntax might be wrong, is it possible for you to post the changes thanks
Re: Help on foreach loop
by jockel (Beadle) on Sep 09, 2004 at 15:05 UTC
    Hi vroom

    Hmm.. There are many strange things in your script
    (atleast strange for me)
    @ARGV = <$responseDir/*>; while (<>) { $newFile = $ARGV, last if /^"$commonField"$/; }

    Does the @ARGV = <$responseDir/*>; acctually work?
    Why while (<>) when u check against that but
    assigning from $ARGV (which hasn't anything to do with @ARGV)

    well that was just a few questions ...

    Good luck

    /jocke
      yes that did work, the problem comes when it iterates through the next foreach loop, it dosen't change the value of $newFile

        The preview page shows "by vroom" (as the edit node, I'm sure) so that might be it.