Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help with pattern matching/replacing 1

Status
Not open for further replies.

dimitad

Programmer
Joined
Sep 16, 2006
Messages
10
Location
US
Hi all,

I need to do a very simple replace on a piece of text but the problem is first of all my code needs to find the required string then based on that string look up an id in a database and then replace the string with the following url:

# where $id is from the db.

This is what I have so far (assume all variables are already initialized to a text file):

foreach (@data) {
if ( m/stringtofind/gi ) {
$string_id{$&} = process_string($&);
}
}

process_string simply takes the string looks it up in the db and returns the id. At this point I need some help ... not sure how to replace the stringtofind with the above url. If i do s/stringtofind/ nothing happens, maybe the $_ variable needs to be reset but I cant find out how.

Thanks for any help!
 
maybe:

Code:
foreach (@data) {
   if ( s/(stringtofind)/process_string($1)/ie ) {
      print "new string: $_\n";
   }
   else {
      print "nothing changed: $_\n";
   }
}

- Kevin, perl coder unexceptional!
 
That works only thing is the functino process_string returns the id of the string. What is the correct way to append that to the url .. something like:

s/(stringtofind)/
the above gives an error of course ...

Thank you for your help!
 
Code:
foreach (@data) {
   if ( s/(stringtofind)/'[URL unfurl="true"]www\.example\.com\/?id='[/URL] . process_string($1)/ie ) {
      print "new string: $_\n";
   }
   else {
      print "nothing changed: $_\n";
   }
}

- Kevin, perl coder unexceptional!
 
You're welcome [wink]

- Kevin, perl coder unexceptional!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top