[plug] Perl problem
Arie Hol
arie99 at ozemail.com.au
Sun Oct 17 21:49:59 WST 2004
Greetings to all Perl gurus,
I have a problem with a Perl script :
The script reads in a list of filenames from one directory, then another list of filenames from
another directory - then joins the two lists into one array - and the second list goes into another
array one its' own.
Using nested for loops - the script then parses all the files in the second array looking for the
filenames from the first array (as hyperlinks) - when it finds them - it is supposed to "change"
the file name in the hyperlink to "deadlink.htm".
Syntax and logic seem to be OK and everything appears to work correctly but when the output files
are checked - the hyperlinks are still in their original state.
The following sample is the 'while' loop that parses each file (one line at a time) :
<sample>
# Parse input file to match filename to hyperlink
while ($line = <$IN_FILE>)
{
$line_count += 1 ;
chomp($line) ;
if ( $line =~ m/${tag}/ig )
{
$found += 1 ;
my $test = $line ;
$line =~ s/${tag}/deadlink\.htm/ig ;
print $OUT_FILE $line."\n" ;
print $OUT_FILE_2 $outfile."\n".$test."\n".$line."\n\n" ;
#break();
}
else
{
print $OUT_FILE $line."\n" ;
} # End of while loop
</sample>
The problem I have is that I cannot see where it is going wrong :
To try and find out why - I inserted an 'if' block to test the output - see the above
"if ( $line =~ m/${tag}/ig ) " block
In this block I split the direction of the output to $OUT_FILE and $OUT_FILE_2.
$OUT_FILE_2 accumulates the output from all files processed receiving the following inputs - the
name of the file being processed ($outfile) , the original line ($test) before substitution and the
line ($line) after substitution - this output is sent to a file called "results.txt" and all the
output is just as I hoped it would be.
$OUT_FILE is the output file generated during each iteration through the while loop - and should
receive the substituted lines - but checking the output files after processing shows that the
original line has been written to the file instead of the modified line.
I have tried changing the logic of the process and many other sequential changes. I even thought
that maybe the system didn't like using variables in the regular expressions -
More mystery :
If I insert a break() in the "if ( $line =~ m/${tag}/ig ) " block, just before the closing brace
of the block (see above) - the file fragment that is generated before the break occurs - contains
the desired result.
Sorry about the long winded descriptions - but I have been at it for days trying to nut it out -
with no success - can any one help me here.
Please ! Please ! Please ! Please ! Please !
Regards Arie
>--------------------------------------------------------------<
For the concert of life, nobody has a program.
>--------------------------------------------------------------<
More information about the plug
mailing list