Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be.

Similar presentations


Presentation on theme: "Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be."— Presentation transcript:

1 Dynamic Arrays Lecture 4

2 Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be increase/ decrease while the program is being executed. Each element is a scalar variable An array in perl is declared as follows:

3 Print Array contents Declaration of an array in perl ; empty array efine (declare and assign values) an array = (‘ATC’, ‘GTC’, ‘AAT’); – This Array contains 3 strings (set of 3 letters: codons!!! Print the values of a non-empty array – Using “For” loops 3 possible ways: If know number of elements of array: standard for-loop If we do not know the number of elements we can: 1.Get length of array and use it with a for loop 2.Foreach - loop

4 3 ways to access elements of array PrintArray.pl example Sample Code: = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', 'CTATGCGGTA', 'ATCTGACCTC' ); #print values using standard for loop for (my $index = 0; $index < 5; $index++) {print "$sequences[$index]\n"; } for (my $index = 0; $index < ; $index++) foreach my ) { print "$value\n"; }

5 Input (add) elements to array Perl uses dynamic array which means you can add/remove elements when the program is running. This is a very useful attribute of Perl The elements can be added to the beginning or end of the array. Step 1: – Create an can be empty or contain data Step 2 – Add elements to the array using the: Push function : adds to end of an array : $sequence; Unshift function: adds to the beginning of an array: $sequence; – See Example PushUnshift.pl – This adds elements enter by the user to the end/beginning of the array.

6 Remove elements from an array To remove an element from an array you can use two functions (reserved words). – Pop removes last element of array and assigns it (returns) to a variable $last = – Shift: removes and returns the first element of an array. $first = – Refer to PopShift.pl

7 Remove Elements: splice and slice To “assigning and remove” element(s) from anywhere in the array: = 1, 2 The first value is where the splice begins and the second is how many elements are to be removed = [1.. 3] = (‘a’, ‘t’, ’g’,’c’) What is the value after splice – slicing (assigning without removal): = = (‘a’, ‘t’, ’g’,’c’) What is the value – Refer: splice_slice.pl

8 Dynamic Arrays Other Array operations (refer to : splice_slice.pl) – $one_seq {zero based array} The 3 rd element of the array is assigned to scalar variable All the elements of one array are assigned to the other

9 Arrays and Files The advantage of using perl’s dynamic arrays is when you need to read the contents of a file of different lengths. – Use a while loop – while { – chomp; – – An approach is “slurping”: (slurping will not remove the carriage return) = ;

10 Arrays: two more functions Sort Function (sorts in alphabetical order) – Be aware of Capital letters as ‘A’ is not considered the same as ‘a’ – Can use uc (converts to upper case) or lc (convert to lower case) = Reverse Function: reverses the elements (does not reverse the contents of the elements) = The reverse function can also be used with scalar variables: – $reverse = reverse $string; Sort_Reverse.pl

11 Array and Lists Lists are sets of constants or variables. – Values of a list assigned to any array = (’192a8’,’18c10’,’327h1’,’201e4’); – Values in an array assigned to a list of variables ($first,$second,$third) – The can be used like arrays but you refer to them by variable name as opposed to element number

12 Exercise 1 #! /usr/bin/perl # The 'pushpop' program - pushing, popping, shifting and = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', 'CTATGCGGTA', 'ATCTGACCTC' ); print $last = print $first = print $last; print ( $first, $last ); What is the expected output (run code to confirm)

13 Exercise 2 Read the contents of a 2 FASTA Files into an 2 arrays via while loop and slurping. Print the contents of each Remove the first element (line) of each array. Reverse the second array Print the contents of the second array. Combined both arrays and print the contents to a third file which you will call file3.fasts

14 Exercise 3 Prompt the user for the name of 2 fasta files and read their contents into 1 array. splice the descriptor line (the first line) of each file from the array and print the contents to confirm they have been removed. Ask the user to add three new sequences to the end of the array Sort the array in reverse order Ask the user to input a descriptor line stating their name and place of work ; e.g. “Denis Manley D.I.T. Kevin Street” Add this descriptor line to the beginning of the array. Print the contents the array to a file and print a report to the screen stating how many characters and lines were written to the file.


Download ppt "Dynamic Arrays Lecture 4. Arrays In many languages the size of the array is fixed however in perl an array is considered to be dynamic: its size can be."

Similar presentations


Ads by Google