140 likes | 350 Views
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 increase/ decrease while the program is being executed. Each element is a scalar variable An array in perl is declared as follows :
E N D
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 increase/ decrease while the program is being executed. • Each element is a scalar variable • An array in perlis declared as follows: • @array_name
Print Array contents • Declaration of an array in perl • @sequences ; empty array • efine(declare and assign values) an array • @sequences = (‘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: • Get length of array and use it with a for loop • Foreach - loop
3 ways to access elements of array • PrintArray.pl example Sample Code: • my @sequences = ( '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 < scalar(@sequences) ; $index++) • foreach my $value(@sequences ) • { print "$value\n"; • }
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 array: @sequences; can be empty or contain data • Step 2 • Add elements to the array using the: • Push function : adds to end of an array : • push @seq, $sequence; • Unshift function: adds to the beginning of an array: • unshift@seq, $sequence; • See ExamplePushUnshift.pl • This adds elements enter by the user to the end/beginning of the array.
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 = pop @sequences; • Shift: removes and returns the first element of an array. • $first = shift @sequences; • Refer toPopShift.pl
Remove Elements: splice and slice • To “assigning and remove” element(s) from anywhere in the array: • @removed = splice @seq, 1, 2 • The first value is where the splice begins and the second is how many elements are to be removed • @removed = splice @seq, [1 .. 3] • If @seq= (‘a’, ‘t’, ’g’ ,’c’) • What is the value of @removed and @seq after splice • slicing (assigning without removal): • @slice = @seq[1,2]; • @seq = (‘a’, ‘t’, ’g’ ,’c’) • What is the value of @slice and @seq • Refer: splice_slice.pl
Dynamic Arrays • Other Array operations (refer to : splice_slice.pl) • $one_seq = @sequences[2] {zero based array} • The 3rd element of the array is assigned to scalar variable • @seq = @sequences; • All the elements of one array are assigned to the other • @combined = (@seq, @seq2)
Arrays and Files • Theadvantage 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<MYFILE>{ • chomp; • push(@arr,$_);} • An approach is “slurping”: (slurping will not remove the carriage return) • @arr = <MYFILE>;
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) • @seq = sort(@sequences); • Reverse Function: reverses the elements (does not reverse the contents of the elements) • @sequences = reverse(@sequences) • The reverse function can also be used with scalar variables: • $reverse = reverse $string; • Sort_Reverse.pl
Array and Lists • Lists are setsof constants or variables. • Values of a list assigned to any array • @clones = (’192a8’,’18c10’,’327h1’,’201e4’); • Values in an array assigned to a list of variables • ($first,$second,$third) = @clones; • The can be used like arrays but you refer to them by variable name as opposed to element number
Exercise 1 • #! /usr/bin/perl • # The 'pushpop' program - pushing, popping, shifting and unshifting. • @sequences = ( 'TTATTATGTT', 'GCTCAGTTCT', 'GACCTCTTAA', • 'CTATGCGGTA', 'ATCTGACCTC' ); • print "@sequences\n"; • $last = pop @sequences; • print "@sequences\n"; • $first = shift @sequences; • print "@sequences\n"; • unshift @sequences, $last; • print "@sequences\n"; • push @sequences, ( $first, $last ); • print "@sequences\n"; • @combine = (@sequences, @sequences); • What is the expected output (run code to confirm)
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
Exercise 3 • Prompt the user for the name of 2fasta 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.