search and replace question - help!

Discussion in 'Perl Misc' started by jac, May 23, 2006.

  1. jac

    jac Guest

    hi everyone...

    i have this code..:
    --------------------------------
    my $pointer="encode";
    my $dir="/home/elizar/files";

    open (ELI, "$dir/silabs") || die "Can't open file. Reason $! \n";
    open (ZAR, ">$dir/silabs.new") || "Can't write file. Reason: $! \n";
    while (<ELI>) {
    # print "$_";
    if ($_ =~ m/$pointer/)
    {
    $_ =~ s/$ww/$ww+1/g;
    }
    print ZAR $_ ;
    }
    close (ZAR);
    close (ELI);
    system "cp $dir/silabs $backup/silabs_$date";
    system "mv $dir/silabs.new $dir/silabs";
    ----------------------------------


    what i want it to replace $ww with $ww+1, that is, if $ww=20,
    replaced it with 21.. its not working.. its been bugging me..

    but when:
    $_ =~ s/20/21/g;

    works fine.

    What am i missing?

    THanks!

    jac
    jac, May 23, 2006
    #1
    1. Advertising

  2. jac

    Mirco Wahab Guest

    Hi jac
    > what i want it to replace $ww with $ww+1, that is, if $ww=20,
    > replaced it with 21.. its not working.. its been bugging me..
    >
    > but when:
    > $_ =~ s/20/21/g;
    >
    > works fine.
    >
    > What am i missing?


    Maybe it works if you use
    the [e]val mode of the substitute context:

    instead of
    $_ =~ s/20/21/g;

    you could use:
    s/ (\d) / $1 + 1 /egx;

    BTW: m/../ and s/../ work on $_ by default.

    Example:

    ...
    $text = " this is a bag with 20 apples\n";
    print $text;
    $_ = $text;

    s/ (\d+) / $1 + 1 /exg;
    print;
    ...

    prints:
    this is a bag with 20 apples
    this is a bag with 21 apples

    Regards

    Mirco
    Mirco Wahab, May 23, 2006
    #2
    1. Advertising

  3. jac

    jac Guest

    thanks plenty Mirco Wahab!
    you guys will be hearinglot from me..
    im trying to understand perl.. im a perl virgin.. ;-)

    thanks!
    jac
    jac, May 24, 2006
    #3
  4. jac

    David Squire Guest

    jac wrote:
    > thanks plenty Mirco Wahab!
    > you guys will be hearinglot from me..


    In that case you should read *and observe* the posting guidelines of
    this group (scan the last week's posts). If you don't observe them (e.g.
    quoting context when you respond to post), you will soon find yourself
    shouting in the wilderness.

    DS
    David Squire, May 24, 2006
    #4
  5. jac

    jac Guest

    Bernard El-Hagin wrote:
    > "jac" <> wrote:
    >
    > > hi everyone...
    > >
    > > i have this code..:
    > > --------------------------------
    > > my $pointer="encode";
    > > my $dir="/home/elizar/files";
    > >
    > > open (ELI, "$dir/silabs") || die "Can't open file. Reason $! \n";
    > > open (ZAR, ">$dir/silabs.new") || "Can't write file. Reason: $!\n";

    >
    >
    > That's not your real code, Shirley.


    right.. of course (and you have to say that huh.. :)
    when i said 'i have this code' im saying that im using it and
    not wrote it.. :)

    Anyway, thanks! Like i said, im a perl virgin and really appreciate
    any help..

    regards,
    jac
    jac, May 24, 2006
    #5
  6. jac

    jac Guest

    oops, sorry.. did that came out bad... i was just trying to post a
    smile.
    from now on, only perl post.

    Thanks,
    jac
    jac, May 24, 2006
    #6
  7. jac

    jac Guest

    David Squire wrote:
    > jac wrote:
    > > thanks plenty Mirco Wahab!
    > > you guys will be hearinglot from me..

    >
    > In that case you should read *and observe* the posting guidelines of
    > this group (scan the last week's posts). If you don't observe them (e.g.
    > quoting context when you respond to post), you will soon find yourself
    > shouting in the wilderness.


    oopss, sorry again.. you we're referring to the quoting of text when
    replying..
    ok got it. Thanks

    jac,
    jac, May 24, 2006
    #7
  8. jac

    David Squire Guest

    jac wrote:
    > David Squire wrote:
    >> jac wrote:
    >>> thanks plenty Mirco Wahab!
    >>> you guys will be hearinglot from me..

    >> In that case you should read *and observe* the posting guidelines of
    >> this group (scan the last week's posts). If you don't observe them (e.g.
    >> quoting context when you respond to post), you will soon find yourself
    >> shouting in the wilderness.

    >
    > oopss, sorry again.. you we're referring to the quoting of text when
    > replying..
    > ok got it. Thanks


    .... as well as the rest of the posting guidelines. Read them.

    DS
    David Squire, May 24, 2006
    #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. techie2go
    Replies:
    3
    Views:
    264
    techie2go
    Sep 10, 2006
  2. Schif Schaf
    Replies:
    12
    Views:
    1,266
    Anthra Norell
    Feb 8, 2010
  3. Abby Lee
    Replies:
    5
    Views:
    398
    Abby Lee
    Aug 2, 2004
  4. Prasad S
    Replies:
    2
    Views:
    229
    Dr John Stockton
    Aug 27, 2004
  5. Ray Muforosky

    string substitution help (search and replace)

    Ray Muforosky, Mar 9, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    123
Loading...

Share This Page