in reply to Re^6: Help on foreach loop
in thread Help on foreach loop

I tried it again, it gives me another filename besides the one that matches, I am trying to head to the below route now
$newFile = `grep -l $commonField $responseDir/*`;
The problem that i am facing now is that when grep dosen't return a match, I don;t know what value is stored in $newFile, how to check if value in $newFile is null or not

Replies are listed 'Best First'.
Re^8: Help on foreach loop
by ysth (Canon) on Sep 09, 2004 at 18:30 UTC
    Read in perlop about `` (aka qx). If you want just one result, use qx in list context: ($newFile) = `...` but be aware that it will have a newline appended, so chomp($newFile) afterwards. I don't have the sense that you are trying pieces of your solution individually; I think you will find this much easier than repeatedly trying small changes in your whole script. I get undef from from `grep -l nosuchmatch *`, for instance.
Re^8: Help on foreach loop
by Anonymous Monk on Sep 09, 2004 at 18:23 UTC
    I had to do a very UGLY hack to make it work, Any suggestions on imporvment will be great
    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"; $newFile = `grep -l $commonField $responseDir/*`; chomp $newFile; print "my newFile to change is $newFile\n"; if ($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"; } }