Workshop in Computational Bioskills - Lesson 2 - Arrays

Workshop in Computational Bioskills - Spring 2011

Lesson 2 - Perl I

Introduction to Perl
Part 1 - Scalars ($)
Part 2 - Lists/Arrays (@)
Part 3 - Hash tables/Associative Arrays (%)

List Data:

In Perl, a list (array) is a variable that holds a list of scalars.
Every element in the list is a scalar variable with its own value.
The indices in the array begin with 0.

Examples of arrays are
(1,2,3);
("bob", 73.4, "");
(); # the empty list.
(1 .. 10); # expands automatically to (1 2 3 4 5 6 7 8 9 10)
("a" .. "z"); # can you guess ?

List Variables:

Array variables are recognized by the "@" prefix. (Just like the "$" sign for scalars).
@a = ("a", "b", "c"); # initialize a list.
@b = ("bob", 13, 2.43e-5);
@c = @b; # copy the entire list to a new one.
@d = ("bubu", @c); # append.
@e = (@c, @d, @c, @a); # no limits here.
($a, @b) = (1,2,3,4);
# will set $a as 1, and @b as the list (2,3,4).

Singletons:

What will this do ?
@a = 1;
Indeed, this will convert the number 1 into the singleton (1).

List Context vs. Scalar Context:

How about this ?
@a = (1,4,3,2);
$b = @a;
That's little more tricky:
Here we initialize a scalar (
$b) with a list (@a).

The list is interpreted in scalar context, setting $b as the number of element in @a.
(in our case, 4).

Think, what's the difference between this and that: ($b) = @a; ?

The "scalar" command:
The same can be done explicitly using the "scalar" command:
$b = scalar(@a);

Accessing Elements in a list:

Is simple !
@a = (7,8,9);
$a[0] == 7; # Is the 1st element of @a zero ? note the "$" sign. a[0] is a scalar, and not a list anymore.
$a[1] = 2; # Set @a to be (7,2,9).

Size of a List:

Unlike C's arrays, in Perl an array can be always resized and expanded (implicitely).
@a = (7,2,9); # The maximal index is now 2 ($a[2] = 9)
$max_index_of_a = $#a;
# = 2
$num_of_elem_in_a = scalar(@a);
# = 3
$a[5] = "hi";
# this will expand the list into (7,2,9,undef,undef,"hi").
$a[100] = undef;
# Expand @a's size to 101.

$a[100] == undef; # What will this do?

List Slicing:

Slicing is a way to access a few elements in list simultaneously:
@b = @a[0,1,5]; # now @b = (7,2,hi);
Is the sliced list always smaller ?

List Operations:

- Get the no. of elements in the list @b:
$num = scalar(@b);
- Get the last index in the list @b:
$max = $#b;
- Reverse a list
@rev = reverse(@b); # @b is unchanged.
- Sort elements in a list in an alphabetic order
@sorted = sort(@b); # @b is unchanged.
We'll learn more about sorting lists later.

Push/Pop elements in a list:

- Insert an element at the beginning of a list:
unshift(@b, "head");
- Pop the first element in a list (and shifting all others one place):
$head = shift(@b);
- Insert an element at the end of a list:
push(@b, "tail");
- Pop the last element of a list:
$tail = pop(@b);
Can you now implement all possible queues ?

Some more I/O:

If you remember (and you should)
$a = <STDIN>;
read one line from the standard input into $a.

Based on your vast experience in Perl, can you guess what
@a = <STDIN>;
does ?

True. It reads all data from the standard input into @a, one line per cell.


This summarizes Arrays. Now Let's continue to Part 3 - Hashes (%)

[Based upon "Learning Perl, 2nd Ed.", by Randel L. Schwartz & Tom Christiansen, Chap. 3]