Data structures exercise

 

 

1. Write a program that prompts the user for 2 vectors (one at a time), with the numbers separated by one or more non-digit characters (\D+).  Each user input goes into a scalar variable that you will need to split into an array.  First check to be sure the two vectors are of equal length and give  an error message if they aren't.  Then pass references to both arrays into a subroutine that does a matrix multiplication on them, returning a scalar value that is the printed out.  Matrix multiplication in this case consists of multiplying a 1 x n matrix (a vector) by an n x 1 matrix (a transposed vector).  If vector 1 is (a1, a2, a3, a4) and vector 2 is (b1, b2, b3, b4), then the matrix product is a1. b1 + a2. b2 + a3. b3 + a4 . b4.

 

 

In the directory /home/bios546 are several files.

 

2. Array of arrays.  The file aoa.txt has 4 lines, each of which has 3 numbers separated by a comma.  This represents a 4x3 matrix (rows x columns).  Create an array of arrays to hold this, with the top level array having 4 elements, each of which is a reference to an anonymous row array.          

                Then, print the elements of the array out in a nice, readable fashion, one row to a line.

                Then, transpose the array:  each row becomes a column and each column becomes a row.  The result is a 3 x 4 matrix.  Print this also.

 

 

3. Hash of arrays.  The file hoa.txt contains 3 lines, that look like:

 

home:httpd,ftp,bios546

 

The first word is a directory under root (/), and the rest of the words, after the colon ( : ) are sub-directories.  Make a hash whose keys are the directory names and whose values are references to anonymous arrays containing the sub-directory names.

                Then, print out each sub-directory so it looks something like:

                                /home/httpd

                                /home/ftp

                                /home/bios546

                                      ...

 

4. Array of hashes.  The file aoh.txt contains several lines, each of which has an enzyme and some of its properties.  A typical line looks like:  name=EcoR1,  site=GAATTC,    source=E. coli.  Note that hash key/value pairs are separated by a =, and that the different pairs are separated by a comma followed by 0 or more spaces.  Create an array of hashes, where each line in the file becomes a separate anonymous hash whose references are the elements of the array.  Then print it out nicely.

 

5.  Hash of hashes.  The file hoh.txt contains properties of DNA, RNA, and protein, each on a separate line that looks like:

 

                DNA: subunits=dNTPs,  strands=2,  synthesis=nucleus

 

Use the type of molecule (before the colon) as the key to the top level hash and the rest as key=vlaue pairs for the inner hashes.  Create the hash of hashes and then print it out.