Perl Basics

Document Sample
Perl Basics Powered By Docstoc
					Perl Programming for Biologists                                                   Spring, 2003



                                  Perl Data Types

Variables

   A variable is a container that holds one or more values that can change throughout a
    program.

   There are 3 types of variables in perl:
        Scalar
        Array
        Asscoiative array (hash/lookup table)


Scalar

   A scalar variable holds a single value, which can be a number or a character string.
   Scalar variable have a dollar sign ($) prefix.
   Examples of scalar variables:

        $EcoRI = 'GAATTC‟;
        $len = 6;

Strings

Strings are sequences of characters (like hello).

Single-Quoted Strings

   Text placed between a pair of single quotes is interpreted literally.
   To get a single quote into a single-quoted string, precede it by a backslash (\).
   To get a backslash into a single-quoted sting, precede the backslash by a backslash.
   Examples of strings:

        „hello‟    # hello
        „can\‟t‟   # can‟t
        „http:\\\\www‟ # http:\\www

Double-Quoted Strings

   The double quote “interpolates” variables between the pair of quotes, which means
    that the variable names within the string are replaced by their current values.
   Examples



Eric Rouchka                                 1                         University of Louisville
Perl Programming for Biologists                                                    Spring, 2003


        $x = 1;
        print „$x‟; # will print out $x
        print “$x”; # will print out 1

   There are several different escape characters that can be printed out:

        \n       Newline
        \t       Tab
        \\       Backslash
        \”       Double quote

Operators on Scalar Variables

   An operator generates a new value from one or more values.

   Arithmetic Operators

        +        Addition
        -        Subtraction
        *        Multiplication
        **       Exponentiation
        /        Division
        %        Modulus

   Assignment operator (=)

        $a = 5;
        $b = “Hello”;
        $c = $a + 2;

   Binary Assignment Operators (+= , -=)

        $a += 2;         # Equivalent to $a = $a + 2;
        $a -= 2;         # Equivalent to $a = $a - 2;

   Autoincrement and Autodecrement Operators (++, --)

        $a++;                     # Equivalent to $a = $a + 1;
        $a--;                     # Equivalent to $a = $a – 1;

   String Concatenation Operator (.)

        $exon1 = "CGGCTGCCACGAGTGCAGGCCAG";
        $exon2 = " GCAACGGGATGGTGAGCCGCT";
        $mRNA = $exon1 . $exon2;
        #mRNA is CGGCTGCCACGAGTGCAGGCCAGGCAACGGGATGGTGAGCCGCT


Eric Rouchka                                 2                          University of Louisville
Perl Programming for Biologists                                                 Spring, 2003


   Numeric and String Comparison Operators

        Comparison                   Numeric                String
        Equal                         ==                      eq
        Not Equal                     !=                      ne
        Less Than                     <                       lt
        Greater Than                  >                       gt
        Less than or equal to         <=                      le
        Greater than or equal to      >=                      ge

   chop() and chomp() string operators

           The chop() operator removes the last character from the string.

                 $x = “world”;
                 chop($x);           # $x is now “worl”

           The chomp() operator removes the newline character from the end of the
            string.

                 $a = “Hello World\n”;
                 chomp ($a);         # $a is now “Hello World”


Arrays

   An array is an ordered list of data.
   An array variable name begins with an at sign (@).

Array Assignment
        @array1 = (1, 2, 3, 4, „five‟);           #array1 has 5 elements
        @array2 = @array1;                        #array2 is the same as array1
        @array3 = (@array1, „six‟);               #array 3 is (1,2,3,4,
                                                  #            „five‟,‟six‟)

Element Access

An array element can be accessed by a numeric index.
Array elements are numbered using sequential integers, beginning at zero.

        @array1 = (1, 2, 3);
        $a = @array1[2];     # $a is 3
        $array1[1] = 6;      # @array1 is (1, 6, 3)
        $array1[0] ++;       # @array1 is (2, 6, 3)




Eric Rouchka                                3                        University of Louisville
Perl Programming for Biologists                                                    Spring, 2003




push() and pop() operators

push() will insert an element at the end of the array. pop() will remove and return
the last element from the array.

        @array1 = (1, 3, 5);
        push(@array1, 2, 4, 6);           # @array1 is (1, 3, 5, 2, 4, 6)
        $last = pop(@array1);             # @array1 is (1, 3, 5, 2, 4)
                                          # and $last is 6

shift() and unshift() operators

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)
        $first = shift(@array1);              # @array1 is (8, 1, 3, 5, 2, 4)

reverse() reverses the order of the elements of the array.

        @array1 = (1, 2, 3);
        @array2 = reverse(@array1);                   # @array2 is (3, 2, 1);
                                                      # @array1 is (1, 2, 3)

join() glues the elements of a list together with a glue string between each element.

        @array1 = ('Hello', 'to', 'me.');
        $sentence = join(' ', @array1);               # $sentence is 'Hello to me.'



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


Multidimensional Arrays

Multidimensional arrays in perl are a little bit more complicated to create.
Two dimensional arrays are accessed by row-order, meaning that the row is listed first.
In order to create a two-dimensional array in perl, if the size is known, it can be created
as follows:




Eric Rouchka                                  4                         University of Louisville
Perl Programming for Biologists                                                   Spring, 2003




@matrix = ( [3,            4,     10],
             [2,           7,     12],
             [0,           3,     4],
             [6,           5,     9],
          );

This creates an array with four rows and three columns. To print the elements of the
array, type:

for($row = 0; $row < 4; $row++) {
   for($col = 0; $col < 3; $col++) {
      print “$matrix[$row][$col] “;
   }
   print “\n”;
}

If we want to first create space for an two-dimensional array, we first need to create space
for a single row, which will have numCols columns:

for($col = 0; $col < $numCols; $col++) {
   push @rowMatrix, “0”;
}

Then we need to create a matrix of these single dimension row matrices:

for($row = 0; $row < $numRows; $row++) {
   push @matrix, [ @rowMatrix ];
}

Now we have a two dimensional array filled with 0’s with of size [numRows] x
[numCols].




Eric Rouchka                                 5                         University of Louisville

				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:6
posted:5/3/2011
language:English
pages:5