| |||||||||||
| |||||||||||
finding position other than 1st using index Posted by naveen (naveen), 31 January 2008 $protein='acgtacgt';#suppose this is my string. i want to find the position of the 2nd cg which occurs in this string $pos=index($protein,'cg')+2; #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 replythe 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 www.opentalk.org.uk and
archived here for reference. To jump to the archive index please
follow this link.
edit your own (not yet published) comments Average page ranking - 5.0 |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho |