467,169 Members | 986 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,169 developers. It's quick & easy.

Pathern Matching problem

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,
Jul 19 '05 #1
  • viewed: 3388
Share:
7 Replies
Niko wrote:
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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
Gunnar Hjalmarsson wrote:
Niko wrote:

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?


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

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #3
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<CH*********************@newsc.telia.net>...
Niko wrote:
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


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.
Jul 19 '05 #4
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

eval $string; # the " $_ =~ " part is redundant
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;

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #5
Gunnar Hjalmarsson <no*****@gunnar.cc> wrote in message news:<Du*********************@newsc.telia.net>...
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

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


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;


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.
Jul 19 '05 #6
Niko wrote:
Gunnar Hjalmarsson wrote:
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

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

I did try the eval but it didnt work,


Then there is probably some other error in you program.


$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/

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #7
Niko wrote:
Gunnar Hjalmarsson wrote:

So, why don't you eval it?

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

I did try the eval but it didnt work,

What do you mean, "didnt work". You need to be more specific
to get any help here.
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;

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
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Andrew McLean | last post: by
reply views Thread by Eric A. Hall | last post: by
7 posts views Thread by Thomas Sourmail | last post: by
5 posts views Thread by olaufr@gmail.com | last post: by
14 posts views Thread by main() | last post: by
11 posts views Thread by tech | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.