Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
finding position other than 1st using index

Posted by naveen (naveen), 31 January 2008
#suppose this is my string. i want to find the position of the 2nd cg which occurs in this string

#i m using this program to find second  position of the motif but it is not working

or can u please tell me some other way of finding the position of the second cg

Posted by KevinAD (KevinAD), 31 January 2008
I'm not sure if you are asking the right question or using a good example, but for the string you posted, you can use rindex instead  of index:

$motif = 'cg';
$protien = 'acgtacgt';
$pos = rindex $protien,$motif;
print $pos;

which returns the position of the last occurance of the substring. Now if the sting can be like this:

$protien = 'acgtacgtatacgtt';

rindex will not work to find the second occurance of the substring. You have to index the string twice using the first position + the length of the substring:

my $motif = 'cg';
my $protien = 'acgtacgtatacgtt';
my $first_pos = index  $protien,$motif;
my $second_pos = index $protien, $motif, length($motif)+$first_pos;
print $second_pos;

Posted by KevinAD (KevinAD), 2 February 2008
I forgot about the pos() function which could be also, but since it uses a regular expression it might be less efficient than above suggestions:

my $motif = 'cg';
my $protien = 'acgtacgtatacgtt';
while($protien =~ /$motif/g){
   print +(pos($protien) - length($motif)),"\n";

the + is in there only so the print function prints the \n on the end, it's not doing anything else.

Posted by naveen (naveen), 3 February 2008
thank you sir for your reply
the previous solution worked.

Posted by KevinAD (KevinAD), 3 February 2008
Thank you for your feedback. For me it is important to know that the help I give is useful and that it is being taken into consideration, otherwise I will stop helping people that never reply to suggestions I offer. That is my own personal policy, nothing to do with this website or Graham.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

Visitor Ranking 5.0 (5=excellent, 1=poor)

edit your own (not yet published) comments

Average page ranking - 5.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: • WEB: • SKYPE: wellho