Web Solving <[EMAIL PROTECTED]> wrote:

: : Telling us
: : what you expect would aid us in helping you further.
: 
: i simply need my script perform a "search and replace" on a txt
: file loading the word to be searched and the replacing word
: from another file. 

    Manipulating three files in perl is not simple when you
first start out. Let's break your problem down into simpler
parts.

  1 - Retrieve search and replacement texts from a file

  2 - Read text from a file.

  3 - Replace text and append output to a third file.


    Now, let's program each task.

#!/usr/bin/perl

use strict;
use warnings;

my $file = 'in.txt';
open FH, $file or die qq(Cannot open "$file": $!);

my @replacements;
while ( <FH> ) {
    next unless m/\|/;

    # limit split to just 2 resulting arguments.
    push @replacements, [ split /\|/, $_, 2 ];
}

close FH;

print join "\t\t", @$_ foreach @replacements;

__END__

    I get this. @replacements is called an array of arrays. Each of its
elements is an anonymous array which holds the search word in its first
element and the replacement in its second element. Read perldsc for
more details on perl data structures.

word            <\a href="http:\/\/www\.word.com>word<\/\a>
other word              <\a href="http:\/\/www\.other-word.com>other
word<\/\a>


    Add the second task.

#!/usr/bin/perl

use strict;
use warnings;

my $file = 'in.txt';
open FH, $file or die qq(Cannot open "$file": $!);

my @replacements;
while ( <FH> ) {
    next unless m/\|/;

    # limit split to just 2 resulting arguments.
    push @replacements, [ split /\|/, $_, 2 ];
}

$file = 'in.blah';
open IN, $file or die qq(Cannot open "$file": $!);


while ( my $line = <IN> ) {

}

close IN;

__END__

    You already know how to open files for reading. Note that I use
pretty much the same thing in each open. It's an editor macro. Perl
programmers seem to open a lot of files. :)


    The third task.

    For each line in the file, do a search and replace of each entry
found in the first task. I use \Q and \E to stop perl's regular
expression engine from interpreting any special characters in the
search string. Read perlop: 'Quote and Quote-Like Operators' for
details.

$file = 'testo_new.txt';
open OUT, ">>$file" or die qq(Cannot open "$file": $!);

while ( my $line = <IN> ) {
    foreach my $replacement ( @replacements ) {
        $line =~ s/\Q$replacement->[0]\E/$replacement->[1]/;
    }
    print OUT $line;
}

close OUT;


    Put it all together (not tested).

#!/usr/bin/perl

use strict;
use warnings;

my $file = 'in.txt';
open FH, $file or die qq(Cannot open "$file": $!);

my @replacements;
while ( <FH> ) {
    next unless m/\|/;

    # limit split to just 2 resulting arguments.
    push @replacements, [ split /\|/, $_, 2 ];
}

$file = 'in.blah';
open IN, $file or die qq(Cannot open "$file": $!);

$file = 'testo_new.txt';
open OUT, ">>$file" or die qq(Cannot open "$file": $!);

while ( my $line = <IN> ) {
    foreach my $replacement ( @replacements ) {
        $line =~ s/\Q$replacement->[0]\E/$replacement->[1]/;
    }
    print OUT $line;
}

close IN;
close OUT;

__END__

     As a programmer you need to get used to breaking problems down into
smaller problems and then testing each of those parts. Later, when you
get into object oriented (OO) programming you'll use a slightly different
approach, but for now top-down programming (which is what we did here)
will suffice.


HTH,

Charles K. Clarkson
-- 
Mobile Homes Specialist
254 968-8328


















    



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to