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

hi im trying to search and replace a string in a doc file..the file is getting opened and read but the string is not getting replaced..i dont know what mistake im making..the snippet is

foreach my $file (@scriptfiles) { $i=0;$j=0; my $var; my $filename = "C:\\Users\\user\\Desktop\\hi\\"; $var = $filename."$file"; print $var ; print $file; my $document = Win32::OLE -> GetObject("$var"); print "Extracting Text ...\n"; my @array; my $paragraphs = $document->Paragraphs(); my $enumerate = new Win32::OLE::Enum($paragraphs); while(my $paragraph = $enumerate->Next()) { my $text = $paragraph->{Range}->{Text}; $text =~ s/[\n\r\t]//g; $text =~ s/\x0B/\n/g; $text =~ s/\x07//g; chomp $text; my $Data .= $text; print $text; $text =~ s/7.0E/8.0A/g; close ($file); @array=split(/\.$/,$Data); foreach my $line( @array) { $line =~ s/Search/Replace/g; } } }

Replies are listed 'Best First'.
Re: Replacing in DOC file
by Athanasius (Archbishop) on Jun 21, 2012 at 15:05 UTC

    Hi stallion,

    Two quick observations. The line

    my $Data .= $text;

    occurs within the inner while loop, and because of the my the $Data variable is being created anew (and effectively initialised to $text) on each loop iteration. So, the concatenation has no effect and $Data never grows from one loop iteration to the next.

    Also,

    close ($file);

    is called on each iteration of the while loop, but this file doesn’t seem to be opened anywhere?

    HTH,

    Athanasius <°(((><contra mundum