The current
program demonstrates the use of “If”,
“Elsif” and “Else” operations. We first store a filename in $filename. We
then open the file read all the contents of the file and store them in the
array and close the file. Each value of the array can be independently
addressed by specifying its position. Example as shown in line 6 and 7. The 0th
value (That is the first value of the array. The count starts from (0) of the
array is stored in $line1 and the second value is stored in $line2.
So we now
ask the user to enter any base sequence. The entered value is stored in the variable
$user. Since the user hits the enter button after providing the input, the
value stored in $user contains the enter character which should be removed. For
that purpose we do chomp on the variable.
Now in
line 13, we check if the value entered by the user is present in $line1. The
expression ~/$user/ depicts that the value entered by the user can be a part of
the complete string in $line1. Else you can just use $line1=$user this means to
see if both are equal. But here we just enter one part (substring) of the
complete string, therefore we use the expression as in line 13. If the value
exists in $line1, it prints out “I found it”. If it is not present in the
$line1, we check in $line2. For that reason we use “elsif”. The “elsif”
command is used when the “if”
condition returned false, that means if the above “if” condition fails. If the above “if” condition is true then the “elsif” line is not executed. So if the value entered by the user
is not present in $line1 it checks if it is present in $line2. If it is there
it prints “I found it in line2”. If both the condition fails we then have final
command called else. That means if the above “if” conditions (if and elsif) fails then the final option is “else”. Here it is not checked with
anything it just prints that “I did not find it”
- #if operation
- $filename="sample.seq";
- open(FILE,$filename);
- @line=<FILE>;
- close FILE;
- $line1=@line[0];
- $line2=@line[1];
- print"$line1\n";
- print"$line2\n";
- print"enter the required base seq:
";
- $user=<STDIN>;
- chomp($user);
- if($line1=~/$user/){
- print"I found it\n";
- }
- elsif($line2=~/$user/)
- {
- print"I found it in line2\n";
- }
- else{
- print"I did not find it\n";
- }
- exit;
RESULTS
TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGC
TCCTCGGGTCGATGCGATAGCTAGCTAGATCT
enter the required base seq:
TCT
I found it in line2
---2nd
try--------
TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGC
TCCTCGGGTCGATGCGATAGCTAGCTAGATCT
enter the required base seq:
TGC
I found it