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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Gabriele
Novice

Mar 7, 2016, 9:20 AM

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

Hi,

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:


Code
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)


Laurent_R
Veteran / Moderator

Mar 7, 2016, 9:57 AM

Post #2 of 3 (1485 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?


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



Gabriele
Novice

Mar 7, 2016, 10:03 AM

Post #3 of 3 (1484 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