CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Highlight substring in string



Mar 7, 2016, 9:20 AM

Post #1 of 3 (2089 views)
Highlight substring in string Can't Post


I have a CGI script that basically capture a string (a DNA sequence) which the user types in the search box of my interface page.
In my cgi script i want to take that string and analyze it for the presence of a substring (gaatcc); I want to print out my full string with these substrings highlighted in bold.
My code below does half of the job; but for some reasons it only highlight the first occurence of the substring; i.e. if there is more than 1 gaatcc, it highlights the first one only.

My code below:

my $length = length($sequence); 

for (my $i=0; $i < $length; $i++) {
if (substr($sequence, $i, 6) eq "gaatcc") {
substr($sequence, $i, 6) = '<b>' . substr($sequence, $i, 6) . '</b>';


print "<p>$sequence</p>";

Any help?

(This post was edited by Gabriele on Mar 7, 2016, 9:54 AM)

Veteran / Moderator

Mar 7, 2016, 9:57 AM

Post #2 of 3 (2077 views)
Re: [Gabriele] Highlight substring in string [In reply to] Can't Post

I can't test a CGI script right now, but does it work any better if you do as in the example below?

$sequence =~ s{gaatcc}{<b>gaatcc</b>}g; 
print "<p>$sequence</p>";


Mar 7, 2016, 10:03 AM

Post #3 of 3 (2076 views)
Re: [Laurent_R] Highlight substring in string [In reply to] Can't Post

Thank you very much; This works!

I have been trying to sort this out for the past 4 hours....rookie's troubles..thanks again


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives