Pathern Matching problem

N

Niko

Hi ,

I am writing script using multiple pattern matching.
I need to replace the $_ =~ few times.

Example:

sub xx {

open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine

xx ();

The problem is that the $_ doesn't execute the pattern matching.

What am I doing wrong?

Thanks a lot,
 
G

Gunnar Hjalmarsson

Niko said:
I am writing script using multiple pattern matching.
I need to replace the $_ =~ few times.

Example:

sub xx {

open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== here is the problem
print TEMPF $_;
}
close (TEMPF);
}

##MAIN##

$string='s/(<RE>).*(<\/RE)/\1xxxx\2/g'; <== this pattern works fine

Then you don't have a pattern matching problem at all, right?

But one problem you have is that you run your code without warnings
enabled: $1 etc. is prefered before \1 etc. at the right side of the
s/// operator.
The problem is that the $_ doesn't execute the pattern matching.

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant
 
G

Gunnar Hjalmarsson

Gunnar said:
Then you don't have a pattern matching problem at all, right?

Or maybe you have... What do you think happens if there are more than
one occurrences of <RE>something</RE> at the same line?
 
N

Niko

Gunnar Hjalmarsson said:
Then you don't have a pattern matching problem at all, right?

But one problem you have is that you run your code without warnings
enabled: $1 etc. is prefered before \1 etc. at the right side of the
s/// operator.


So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant

HI,

First thanks for replay,
I did try the eval but it didnt work,
can you give me an example how to use it in my case ?

Thanks again.
 
G

Gunnar Hjalmarsson

Niko said:
I did try the eval but it didnt work,

Then there is probably some other error in you program.
can you give me an example how to use it in my case ?

If it "does not work" for you, show us a short but *complete* program
with sample data that we can copy and run, where strictures and
warnings have been enabled, and that does not output the expected result.

If you do, we can help you fix it.

Another thing is that I'm not sure this eval() approach is a good
choice. It's probably better to use the qr// operator.

my $re = qr|(<RE>).*?(</RE)|;

s/$re/$1xxxx$2/g;
 
N

Niko

Gunnar Hjalmarsson said:
Then there is probably some other error in you program.


If it "does not work" for you, show us a short but *complete* program
with sample data that we can copy and run, where strictures and
warnings have been enabled, and that does not output the expected result.

If you do, we can help you fix it.

Another thing is that I'm not sure this eval() approach is a good
choice. It's probably better to use the qr// operator.

my $re = qr|(<RE>).*?(</RE)|;

s/$re/$1xxxx$2/g;

Hi ,

This is the full script:

#!/bin/perl
###############
# Global Vars #
###############
#

$OV_CONF_FILE = "/Users/conf.xml";
$UNINST = $ARGV[0];

#############
# Subroutins #
#############
#
sub Replace_Sub {
open (FILE,$S_FILE) || die "cant open file";
open (TEMPF,"> $T_FILE");
while (<FILE>) {
$_ =~ $string; <== this is the problem
print TEMPF $_;
}
close (TEMPF);
close (FILE);
}

########
# Main #
########
#

## Update loopback to on in ov.conf file - DR-DR-0-026-370
$S_FILE = $OV_CONF_FILE;
$T_FILE = "$S_FILE.new_sec_kit";
$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern

if ( $UNINST eq "remove") {
if (! -f "$S_FILE.orig_sec_kit") {
print "The $S_FILE Original file exist or already in orig state ...\n"
}else{
`mv -f $S_FILE.orig_sec_kit $S_FILE`;
}
}else{
if (-f "$S_FILE.orig_sec_kit") {
print "Modified $S_FILE file already exist....\n"
}else{
Replace_Sub();
`cp -p $S_FILE $S_FILE.orig_sec_kit`;
`mv -f $T_FILE $S_FILE`;
}
}



The content of the file is:

..
<RE>blabla</RE>
..
..

That's all.

Tahnks a lot.
 
G

Gunnar Hjalmarsson

Niko said:
$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern
----------^-----------------------------^

Replace those double quotes with single quotes.

There is a lot more to say about your program, but I'm not in the
mood, since you ignored the advices I already gave you.

If you want to learn Perl, start here:

http://learn.perl.org/
 
J

Joe Smith

What do you mean, "didnt work". You need to be more specific
to get any help here.

Did you try that?
This is the full script:

#!/bin/perl

You did not put in
use strict;
use warnings;
as Gunnar asked. You must do this if you seriously want assistance.
$_ =~ $string; <== this is the problem

That is not "a *complete* program with sample data".
What you posted doesn't even compile.
$string="s/(<RE>).*(<\/RE>)/\1xxxx\2/g"; <== the pattern

Inside a double-quoted string, "\1" and "\2" are *not* the
same as what is used in a regex. Two people have told you
things you can do instead, but you have ignored their advice. Why?

Learn how to use qr() or eval($perl_command).
The content of the file is:
.
<RE>blabla</RE>

You should not be describing the file in English.
You should include the file verbatim after the __DATA__ delimiter.
-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top