Pathern Matching problem

Discussion in 'Perl' started by Niko, Aug 10, 2004.

  1. Niko

    Niko Guest

    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,
    Niko, Aug 10, 2004
    #1
    1. Advertising

  2. 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
    Gunnar Hjalmarsson, Aug 10, 2004
    #2
    1. Advertising

  3. 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
    Gunnar Hjalmarsson, Aug 10, 2004
    #3
  4. Niko

    Niko Guest

    Gunnar Hjalmarsson <> wrote in message news:<CH0Sc.100450$>...
    > 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.
    Niko, Aug 10, 2004
    #4
  5. 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
    Gunnar Hjalmarsson, Aug 10, 2004
    #5
  6. Niko

    Niko Guest

    Gunnar Hjalmarsson <> wrote in message news:<Du4Sc.100464$>...
    > 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.
    Niko, Aug 11, 2004
    #6
  7. 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
    Gunnar Hjalmarsson, Aug 11, 2004
    #7
  8. Niko

    Joe Smith Guest

    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
    Joe Smith, Aug 11, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roedy Green

    A Pixel Matching Problem

    Roedy Green, Nov 19, 2005, in forum: Java
    Replies:
    0
    Views:
    520
    Roedy Green
    Nov 19, 2005
  2. falcon
    Replies:
    10
    Views:
    18,550
    Roedy Green
    Feb 24, 2006
  3. timmu

    String Matching Problem (NUXI problem) ?

    timmu, May 18, 2006, in forum: C Programming
    Replies:
    6
    Views:
    593
    Malcolm
    May 20, 2006
  4. Marc Bissonnette

    Pattern matching : not matching problem

    Marc Bissonnette, Jan 8, 2004, in forum: Perl Misc
    Replies:
    9
    Views:
    220
    Marc Bissonnette
    Jan 13, 2004
  5. Bobby Chamness
    Replies:
    2
    Views:
    214
    Xicheng Jia
    May 3, 2007
Loading...

Share This Page