Login

Join for Free!
117074 members
table of contents table of contents

Biology Articles » Bioinformatics » Introductory Workbook on Perl for Biology Students » 10. If and else operations

10. If and else operations
- Introductory Workbook on Perl for Biology Students

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”

 

  1. #if operation
  2. $filename="sample.seq";
  3. open(FILE,$filename);
  4. @line=<FILE>;
  5. close FILE;
  6. $line1=@line[0];
  7. $line2=@line[1];
  8. print"$line1\n";
  9. print"$line2\n";
  10. print"enter the required base seq: ";
  11. $user=<STDIN>;
  12. chomp($user);
  13. if($line1=~/$user/){
  14. print"I found it\n";
  15. }
  16. elsif($line2=~/$user/)
  17. {
  18. print"I found it in line2\n";
  19. }
  20. else{
  21. print"I did not find it\n";
  22. }
  23. 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


rating: 3.71 from 112 votes | updated on: 30 Jan 2009 | views: 92536 |

Rate article:







excellent!bad…