Ask a Question related to PERL Miscellaneous, Design and Development.

  1. #1

    Default A newBie Query

    Hello grp,
    I am searching for a perfect match of a string in a line and want to
    replace all occurances of the string with a new string and also want a count
    of the replacements

    What I mean by a perfect match is that it should not be a substring of any
    other string,should be independent.

    $a is my string to be searched
    $cnt = ($line =~ s/$a\w+/$tobereplaceed)

    I dont get a correct output.

    PLz help

    Thaanx in advance
    Rgds,
    Naren.


    Naren Guest

  2. Similar Questions and Discussions

    1. Newbie about grouping query
      In my mdb, the data is as below : ProductID Qty Name Month CID...
    2. datagrid query-newbie
      i)How to set foucs to a datagrid programatically? Is there any method to do so? Can we redraw a datagrid ? I want to update a datagrid. But I...
    3. A newbie query about Soap4R and dot net web services
      Can anyone help me with this Soap4r problem? I have created a simple web service in dot net using Visual studio and have modified a sample...
    4. [newbie] Array and query
      Hi to all! :) This week i begin to learn php, but now i have a problem... I made a form in which there is the possibility of choosing some...
    5. Help ASP newbie with webhits query
      I have been asked by my office to set up a Intranet site using ASP. (I know VB6, so it must be simple!). The basics are done, including using MS...
  3. #2

    Default Re: A newBie Query

    On Wed, 17 Sep 2003 12:25:26 +0530
    "Naren" <narendranath.ts@in.bosch.com> wrote:

    # Hello grp,
    # I am searching for a perfect match of a string in a line and want to
    # replace all occurances of the string with a new string and also want a count
    # of the replacements
    #
    # What I mean by a perfect match is that it should not be a substring of any
    # other string,should be independent.
    #
    # $a is my string to be searched
    # $cnt = ($line =~ s/$a\w+/$tobereplaceed)

    $cnt = ( $line =~ s/\b$a\b/$tobereplaced/g )
    ^^ ^^ ^^

    \b - asserts for a word boundary
    g - to repeat the search and replace globally for the string.

    Also, you might run into some problems when $a has some meta
    characters like +, ?, (, ). Use Quotemeta to transform them.

    perldoc perlre for more details.


    Regards,
    Thens.

    Thens Guest

  4. #3

    Default Re: A newBie Query

    Naren <narendranath.ts@in.bosch.com> wrote:
    > Subject: A newBie Query

    Please put the subject of your article in the Subject of your article.

    > I am searching for a perfect match of a string in a line and want to
    > replace all occurances of the string with a new string and also want a count
    > of the replacements

    Use s///g;

    > What I mean by a perfect match is that it should not be a substring of any
    > other string,should be independent.

    The count must always be one (or zero) if it is not a substring
    of any other string, as whatever sub-part you match must necessarily
    be a substring of the entire string being searched.

    I think you've used the wrong terminology, but I can't divine
    what term you were looking for...

    > $a is my string to be searched

    No it isn't. Your code below searches $line, not $a.

    > $cnt = ($line =~ s/$a\w+/$tobereplaceed)
    ^^
    ^^
    > I dont get a correct output.

    A syntax error is never the correct output!

    What output _do_ you get?

    What output were you expecting to get?

    Why were you expecting to get that?

    > PLz help

    We don't know what data is in $a.

    We don't know what data is in $line.

    We don't know what data is in $tobereplaceed.

    We do not know what the input is.

    We don't know what the code is.

    We do not know what the output is intended to be.


    Please help the helpers to help you by helpfully providing enough
    information for the helpers to be _able_ to help you.

    Have you seen the Posting Guidelines that are posted here frequently?



    use PSI::ESP;

    Maybe this is what you are looking for:

    my $cnt = $line =~ s/\b$a\b/$tobereplaceed/g;

    or maybe:

    my $cnt = $line =~ s/\b\Q$a\E\b/$tobereplaceed/g;


    --
    Tad McClellan SGML consulting
    [email]tadmc@augustmail.com[/email] Perl programming
    Fort Worth, Texas
    Tad McClellan Guest

  5. #4

    Default Re: A newBie Query

    Hello Grp,
    Thanks for all the suggestions and advices.I could do the required
    functionality.I shall specify the problems in a detailed manner in future.

    Thanks again

    Rgds,
    Naren.


    Naren Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139