Overview
In this example, you will learn how to write a program that will find the position of the first occurrence of ATG in a sequence.
Example Code
OutPut
The length of the sequence is 94
The position of first ATG occurrence is : 19
Decode
>>Line 13: $length= length($sequence);
length(STRING) is a perl function that will return the length of the STRING. In this case, length($sequence) returns the length of $sequence which is 94 and store this value into the variable $length.
>>Line 22: $firstOccurrence=index($sequence,"ATG",0)
index(STRING,SUBSTRING,POSITION) finds the SUBSTRING in a STRING and returns its index. POSITION indicates where to start looking for the SUBSTRING.
Note: The index of the first character in a String is 0, the second character 1, the third character 2, etc
In our case, index($sequence,"ATG",0) finds the first occurrence of "ATG" in $sequence starting from index 0. index($sequence,"ATG",44) will find the first occurrence of "ATG" in $sequence starting from index 44.
Note: index(STRING, SUBSTRING) will assume that POSITION is 0 . eg. index($seqeunce,"ATG") is the same as index($sequence,"ATG",0)
Exercise Two
>> Modify the above example to find the second occurrence of "ATG" in the sequence