Conditional tests and matching braces
5.1.1.1 Conditional tests and matching braces
Two more comments are in order about these statements and their conditional tests. First, there are several tests that can be used in the conditional part of these statements. In addition to numeric equality == as in the previous example, you can also test for inequality = , greater than , less than , and more. Similarly, you can test for string equality using the eq operator: if two strings are the same, its true . There are also file test operators that allow you to test if a file exists, is empty, if permissions are set a certain way, and so on see Appendix B . One common test is just a variable name: if the variable contains zero, its considered false ; any other number evaluates to true . If the variable contains a nonempty string, it evaluates to true ; the empty string, designated by or , is false . Second, notice that the statements that follow the conditional are enclosed within a matching pair of curly braces. These statements within curly braces are called a block and arise frequently in Perl. [1] Matching pairs of parentheses, brackets, or braces, i.e., , [ ], , and { }, are common programming features. Having the same number of left and right braces in the right places is essential for a Perl program to run correctly. [1] As something of an oddity, the last statement within a block doesnt need a semicolon after it. Matching braces are easy to lose track of, so dont be surprised if you miss some and get error messages when you try to run the program. This is a common syntax error; you have to go back and find the missing brace. As code gets more complex, it can be a challenge to figure out where the matching braces are wrong and how to fix them. Even if the braces are in the right place, it can be hard to figure out what statements are grouped together when youre reading code. You can avoid this problem by writing code that doesnt try to do too much on any one line and uses indentation to further highlight the blocks of code see Section 5.2 . [2] [2] Some text editors help you find a matching brace for instance, in the vi editor, hitting the percent key over a parenthesis bounces you to the matching parenthesis. Back to the conditional statements. The if-else also has an if-elsif-else form, as in Example 5-1 . The conditionals, first the if and then the elsifs, are evaluated in turn, and as soon as one evaluates to true , its block is executed, and the rest of the conditionals are ignored. If none of the conditionals evaluates to true , the else block is IT-SC 73 executed if there is oneāits optional. Example 5-1. if-elsif-else usrbinperl -w if-elsif-else word = MNIDDKL; if-elsif-else conditionals ifword eq QSTVSGE { print QSTVSGE\n; } elsifword eq MRQQDMISHDEL { print MRQQDMISHDEL\n; } elsif word eq MNIDDKL { print MNIDDKL--the magic word\n; } else { print Is \word\ a peptide? This program is not sure.\n; } exit; Notice the \ in the else blocks print statement; it lets you print a double-quote sign within a double-quoted string. The backslash character tells Perl to treat the following as the sign itself and not interpret it as the marker for the end of the string. Also note the use of eq to check for equality between strings. Example 5-1 gives the output: MNIDDKL--the magic word5.1.2 Loops
Parts
» OReilly.Beginning.Perl For Bioinformatics
» The Organization of Proteins
» In Silico Biology and Computer Science
» A Low and Long Learning Curve
» Ease of Programming Rapid Prototyping
» Portability, Speed, and Program Maintenance
» Perl May Already Be Installed No Internet Access?
» Downloading Binary Versus Source Code
» Unix and Linux Macintosh Windows
» Unix or Linux How to Run Perl Programs
» Text Editors Getting Started with Perl
» Finding Help Getting Started with Perl
» Saves and Backups Error Messages
» Individual Approaches to Programming Programming Strategies
» The Design Phase The Programming Process
» Algorithms The Programming Process
» Pseudocode and Code Comments
» Representing Sequence Data Sequences and Strings
» Control Flow Comments Revisited Command Interpretation
» Assignment Print Exit Statements
» Concatenating DNA Fragments Sequences and Strings
» Using the Perl Documentation
» Calculating the Reverse Complement in Perl
» Proteins, Files, and Arrays Reading Proteins in Files
» Arrays Sequences and Strings
» Scalar and List Context Exercises
» Conditional tests and matching braces
» Code Layout Motifs and Loops
» Getting User Input from the Keyboard Turning Arrays into Scalars with join
» Regular expressions and character classes
» Counting Nucleotides Motifs and Loops
» Exploding Strings into Arrays
» Operating on Strings Motifs and Loops
» Writing to Files Motifs and Loops
» Advantages of Subroutines Subroutines
» Arguments Scoping and Subroutines
» Scoping Scoping and Subroutines
» Command-Line Arguments and Arrays
» Subroutines: Pass by Value Subroutines: Pass by Reference
» Modules and Libraries of Subroutines
» use warnings; and use strict; Fixing Bugs with Comments and Print Statements
» How to start and stop the debugger Debugger command summary
» Stepping through statements with the debugger
» Setting breakpoints The Perl Debugger
» Fixing another bug use warnings; and use strict; redux
» Exercises Subroutines and Bugs
» Random Number Generators Mutations and Randomization
» Seeding the Random Number Generator Control Flow
» Making a Sentence Randomly Selecting an Element of an Array
» Formatting A Program Using Randomization
» Select a random position in a string Choose a random nucleotide
» Improving the Design Combining the Subroutines to Simulate Mutation
» Exercises Mutations and Randomization
» A Gene Expression Database Gene Expression Data Using Unsorted Arrays
» Gene Expression Data Using Sorted Arrays and Binary Search
» Gene Expression Data Using Hashes
» Translating Codons to Amino Acids
» The Redundancy of the Genetic Code
» Using Hashes for the Genetic Code
» Translating DNA into Proteins
» FASTA Format Reading DNA from Files in FASTA Format
» A Design to Read FASTA Files
» A Subroutine to Read FASTA Files
» Writing Formatted Sequence Data
» Regular Expressions Restriction Maps and Regular Expressions
» Background Planning the Program
» Restriction Enzyme Data Restriction Maps and Restriction Enzymes
» Logical Operators and the Range Operator
» Finding the Restriction Sites
» Exercises Restriction Maps and Regular Expressions
» Using Arrays Separating Sequence and Annotation
» Pattern modifiers Examples of pattern modifiers
» Separating annotations from sequence
» Using Arrays Parsing Annotations
» When to Use Regular Expressions
» Main Program Parsing Annotations
» Parsing Annotations at the Top Level
» Features Parsing the FEATURES Table
» Parsing Parsing the FEATURES Table
» DBM Essentials Indexing GenBank with DBM
» Overview of PDB Protein Data Bank
» Opening Directories Files and Folders
» Processing Many Files Files and Folders
» Extracting Primary Sequence Parsing PDB Files
» Finding Atomic Coordinates Parsing PDB Files
» The Stride Secondary Structure Predictor
» Parsing Stride Output Controlling Other Programs
» String Matching and Homology
» Extracting Annotation and Alignments
» Parsing BLAST Alignments Parsing BLAST Output
» The printf Function Presenting Data
» here Documents format and write
» Bioperl Tutorial Script Bioperl
» The Art of Program Design Web Programming
» Algorithms and Sequence Alignment Object-Oriented Programming Complex Data Structures
Show more