1 / 19

Perl has three data types

Perl has three data types. $ - Scalar: holds a single value, which can be a number or string, $EcoRI = ‘GAATTC’; @ - Array: stores multiple scalar values [0, 1, 2, etc.] % - Hash: An associative array with keys and values. End of chapter 4-beginning of chapter 5. Arrays.

quinta
Télécharger la présentation

Perl has three data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Perl has three data types • $ - Scalar: holds a single value, which can be a number or string, $EcoRI = ‘GAATTC’; • @ - Array: stores multiple scalar values [0, 1, 2, etc.] • % - Hash: An associative array with keys and values End of chapter 4-beginning of chapter 5

  2. Arrays • An array is an ordered list of data • An array variable begins with an @ • @array1 = (1, 2, 3, four); #array1 has five elements

  3. Element Access • An array element can be accessed via a numeric index • Array elements are numbered sequentially by integers beginning at zero • @array1 = (1, 2, 3); • $a = @array1[2]; #$a is 3 • $array[1] = 6; #@array1 is (1, 6, 3) • $array1[0] ++; #@array1 is (2, 6, 3)

  4. Push and Pop • Push will insert an element at the end of the array. • Pop will remove and return the last element from an array • Push(@array1, 2, 4, 6); #@array1 is (1, 3, 5, 2, 4, 6) • $last = pop(@array1); # $last is 6

  5. Shift and unshift • Shift removes and returns the first element of the array • Unshift inserts an element to the beginning of the array • Unshift (@array1, 7, 8); #@array1 is (7, 8, 1, 3, 5, 2, 4, 6) • $first = shift (@array1); #@array is (8, 1, 3, 5, 2, 4)

  6. Reverse and Join • Reverse: reverses the order of the elements of the array • @array2 = reverse (@array1) #array2 is (4, 2, 5, 3, 1, 8, 7) • Join collapses an array into a single string • @array1 = (‘Hello’, ‘to’, ‘me.’); • $sentence = join(‘’,@array1); • $sentence is ‘Hello to me.’

  7. Retrieving array length • Using an array in a context where a scalar value is expected returns the length of the array • @array1 = (1, 2, 3, 4); • $a = @array1; #$a is 4

  8. Scalar and List context • Example 4-8 • @bases = (‘A’, ‘C’, ‘G’, ‘T’); • Print “@bases\n”; • Output: A C G T • $a = @bases; • Print $a, “\n”; • Output: 4 • ($a) = @bases; • Print $a, “\n”; • Output: A

  9. Working with Files Biological data can come in a variety of file formats and our job is to utilize these files and extract what we want One such file format is FASTA

  10. Opening a file in Perl • Syntax for open a file in perl: • open (FILEHANDLE, “filename”) or die “Can’t open filename\n”; Open  This is a perl function to open a file FILEHANDLE  The string you use to refer to the file throughout your perl script “filename”  The actual name of the file,double quotes are required Or die “”  tells the program to quit if cannot open or find (optional but recommended)

  11. Closing a file in Perl • Close (FILEHANDLE); • Typically, it is good practice to close a file once you open it…

  12. Scalar vs. Array • Example 4-5 provides a simple distinction between use of a scalar variable and an array, read it, but don’t necessarily do it • Also, it shows how you use filehandles in association with your file • < > are input operators, you will become better acquainted with this when we use <STDIN> later

  13. adhI.pep • Supplant NM_021964fragment.pep with adhI.pep, which can be downloaded from the web-site to a folder you need to create on your computer called “BIOS482” • Do Example 4-7 • Practice manipulating arrays using the code on page 51-53

  14. Flow control • Flow control refers to the order in which the statements of a program is executed • Goes from the first statement at the top to the last statement at the bottom • Unless you implement conditional statements and loops

  15. Statement blocks • A sequence of statements can be grouped together by enclosing them in curly braces {} to form a statement block. Each statement block will be executed in the order in which they appear. • Structure { first statement; last statement; }

  16. Conditional statements • Tests for a condition, which is evaluated as true or false. True conditions lead to the execution of statements, false means they’re skipped (or vice versa) • If-elsif-else (Example 5-1)

  17. Loops • Allows repeated execution of a block of statements enclosed in matching curly braces • While • For • Foreach • Do-while • Until, Etc.

  18. Code Layout • Can be a matter of preference, what you can read well • Some examples are on pg 62 of your text • I would let the examples we do in class help you along the way

  19. Finding motifs • Work through example 5-3 • Read pages 63-70 • Homework

More Related