Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple pattern match and replace

Status
Not open for further replies.

Evan145

Technical User
Dec 24, 2001
33
IE
I am having a problem with a regex.
I am trying to parse a file (which has been previously marked with tags) and these tags replaced as variables.

Example input.txt
The quick brown fox jumps over the lazy dog
The stupid learner had trouble with regex


I manually edit input.txt with @tags@ to specify which words are to be variablized.
The quick @brown@ fox jumps over the lazy @dog@
The @stupid@ @learner@ had trouble with @regex@


The input.txt is passed to the script and I want it to be able top do something like this within the script..
Code:
$a = <STDIN>;
$b = <STDIN>;
$c = <STDIN>;
$d = <STDIN>;
.
.

for ($i=1; $i<=10; $i++) {
        open(OUTPUTFILE,">$output");
        print OUTPUTFILE "The quick $a fox jumps over the lazy $b\n";
        print OUTPUTFILE "The $c $d had trouble with $e\n\n";
        close(OUTPUTFILE);
}

Im trying to debug it like this...
while ( my $line = <FILE> ) {
chomp($line);
while ($line =~ s/@(\w*)@/\$i/g) {
print $line,"\n";
}
}



I want the lines resused within the script, but variablised.

Im not looking for solution, just tips on parsing the input lines and replace the matches as ouput to another file OR a variable $line1,$line2,$line3 etc, that can be reused, as shown the the example code.

Thanks in advance!!!
Evan
 
LOL...Just to show Im trying ..lol

I've arrived at this..
while ($line =~ s/@(.*?)@/\$i/g) { print $line,"\n"; }

Only problem is that this only replaces with the same $i variable every match ??? ???

Evan

 
Update

Code:
open FILE, "<$filename" or die "$0: cannot open $filename";

my $num = 1;
my $count;
my @ln;

my $replace = "\$var".$num;

while ( my $line = <FILE> ) {
        chomp($line); 
        while ($line =~ s/@(.*?)@/$replace/) {  
                $replace = "\$var".$num++;    
        }
        $ln[$count++] = $line; 
} 

foreach my $i(@ln) {
      print $i,"\n";
}

Removed global match
But, problem on line 1 with $var1 repeating??

The quick $var1 fox jumps over the lazy $var1
The $var2 $var3 had trouble with $var4
 
I'm confused, I don't know what the output is you are trying to get after opening the input file and running it through your code. You have this:

The quick @brown@ fox jumps over the lazy @dog@
The @stupid@ @learner@ had trouble with @regex@

what output are you trying to get from the above input?
 
ahh, I missed this part of your last post:

The quick $var1 fox jumps over the lazy $var1
The $var2 $var3 had trouble with $var4

which I assume is the output you want. Here is a way sticking with your code for the most part:

Code:
open FILE, "<$filename" or die "$0: cannot open $filename";

my $num = 1;
my @ln;

my $replace = '$var';

while ( my $line = <FILE> ) {
        chomp($line);
        $line =~ s/@([^@]+)@/$replace.$num++/ge;
        push @ln,$line;
}

print map {"$_$/"} @ln;
 
That has helped me immensely
Thanks Kevin,
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top