Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > A newBie Query

Reply
Thread Tools

A newBie Query

 
 
Naren
Guest
Posts: n/a
 
      09-17-2003
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.


 
Reply With Quote
 
 
 
 
Thens
Guest
Posts: n/a
 
      09-17-2003
On Wed, 17 Sep 2003 12:25:26 +0530
"Naren" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
 
Tad McClellan
Guest
Posts: n/a
 
      09-17-2003
Naren <(E-Mail Removed)> 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
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Naren
Guest
Posts: n/a
 
      09-18-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET won't retrieve query results that depend on union query Eric Nelson ASP .Net 5 02-04-2009 10:51 PM
Trying to query the Address table data of AdventureWorks database from Query Analyzer - need help! Learner ASP .Net 1 01-30-2006 08:58 PM
Build dynamic sql query for JSTL <sql:query> Anonymous Java 0 10-13-2005 10:01 PM
xpath query query David Gordon XML 2 05-18-2005 03:33 PM
CAML Query: Multiple Query Fields Issue Jon F. ASP .Net Web Services 0 05-12-2004 08:19 PM



Advertisments