tutorials:perl_exercises
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorials:perl_exercises [2010/08/01 00:11] – chkuo | tutorials:perl_exercises [2010/08/07 00:42] (current) – chkuo | ||
---|---|---|---|
Line 4: | Line 4: | ||
Read from an input file and write the content to an output file. Assume the two filenames are provided in the command line. | Read from an input file and write the content to an output file. Assume the two filenames are provided in the command line. | ||
- | **Hint: | + | **Hint: |
<code bash> | <code bash> | ||
$ cat input.txt | $ cat input.txt | ||
Line 21: | Line 21: | ||
</ | </ | ||
See a sample answer [[tutorials: | See a sample answer [[tutorials: | ||
+ | |||
+ | ===== parse_first_name ===== | ||
+ | Read from an input file (containing a list of full names) and write the first names to an output file. Assume the two filenames are provided in the command line. | ||
+ | |||
+ | **Hint: learn about filehandle and array (or regular expression) before attempting this exercise.** | ||
+ | <code bash> | ||
+ | $ cat input.txt | ||
+ | George Washington | ||
+ | John Adams | ||
+ | Thomas Jefferson | ||
+ | James Madison | ||
+ | James Monroe | ||
+ | $ perl parse_first_name.pl input.txt first_name.txt | ||
+ | $ cat first_name.txt | ||
+ | George | ||
+ | John | ||
+ | Thomas | ||
+ | James | ||
+ | James | ||
+ | </ | ||
+ | See a sample answer [[tutorials: | ||
===== count_first_name ===== | ===== count_first_name ===== | ||
Read an input file that contains several names (assuming one name in each line, the first name and the last name are separate by a space). Count how the number of times each first name appeared in the input. Print the result to STDOUT, sort the names alphabetically. | Read an input file that contains several names (assuming one name in each line, the first name and the last name are separate by a space). Count how the number of times each first name appeared in the input. Print the result to STDOUT, sort the names alphabetically. | ||
- | **Hint: | + | **Hint: |
<code bash> | <code bash> | ||
$ cat input.txt | $ cat input.txt | ||
Line 40: | Line 61: | ||
</ | </ | ||
See a sample answer [[tutorials: | See a sample answer [[tutorials: | ||
+ | |||
+ | ===== write_lines_to_files ===== | ||
+ | Obtain an input file and an output directory from the command line. Produce one output file for each line in the input file, use the line number (i.e., 1, 2, 3, etc) as the filenames. | ||
+ | |||
+ | **Hint: learn about filehandle and directory operation before attempting this exercise.** | ||
+ | |||
+ | <code bash> | ||
+ | $ cat input.txt | ||
+ | George Washington | ||
+ | John Adams | ||
+ | Thomas Jefferson | ||
+ | James Madison | ||
+ | James Monroe | ||
+ | $ perl write_lines_to_files.pl input.txt output/ | ||
+ | $ head output/* | ||
+ | ==> output/1 <== | ||
+ | George Washington | ||
+ | |||
+ | ==> output/2 <== | ||
+ | John Adams | ||
+ | |||
+ | ==> output/3 <== | ||
+ | Thomas Jefferson | ||
+ | |||
+ | ==> output/4 <== | ||
+ | James Madison | ||
+ | |||
+ | ==> output/5 <== | ||
+ | James Monroe | ||
+ | </ | ||
+ | See a sample answer [[tutorials: | ||
+ | |||
+ | |||
+ | ===== combine_files ===== | ||
+ | The opposite of write_lines_to_files; | ||
+ | |||
+ | **Hint: learn about filehandle, directory operation, and pattern matching before attempting this exercise.** | ||
+ | |||
+ | <code bash> | ||
+ | $ head output/* | ||
+ | ==> output/1 <== | ||
+ | George Washington | ||
+ | |||
+ | ==> output/2 <== | ||
+ | John Adams | ||
+ | |||
+ | ==> output/3 <== | ||
+ | Thomas Jefferson | ||
+ | |||
+ | ==> output/4 <== | ||
+ | James Madison | ||
+ | |||
+ | ==> output/5 <== | ||
+ | James Monroe | ||
+ | $ perl combine_files.pl output/ combined.txt | ||
+ | $ cat combined.txt | ||
+ | George Washington | ||
+ | John Adams | ||
+ | Thomas Jefferson | ||
+ | James Madison | ||
+ | James Monroe | ||
+ | </ | ||
+ | See a sample answer [[tutorials: | ||
+ | |||
+ | |||
===== unwrap_fasta ===== | ===== unwrap_fasta ===== | ||
Line 46: | Line 132: | ||
**Hint: learn about the '' | **Hint: learn about the '' | ||
<code bash> | <code bash> | ||
- | cat coding.fasta | + | $ cat coding.fasta |
> | > | ||
ATGAAACGCATTAGCACCACCATT | ATGAAACGCATTAGCACCACCATT | ||
Line 95: | Line 181: | ||
===== count_EcoRI_site ===== | ===== count_EcoRI_site ===== | ||
- | Read a [[http:// | + | Read a [[http:// |
**Hint: learn about the regular expression before attempting this exercise.** | **Hint: learn about the regular expression before attempting this exercise.** | ||
+ | |||
+ | <code bash> | ||
+ | $ cat EcoRI.fasta | ||
+ | >Seq_1 | ||
+ | nnnGAA | ||
+ | TTCnnnGAATTCnnnGaattCnnn | ||
+ | >Seq_2 | ||
+ | nnnGAATTCnnngAa | ||
+ | TtCnnn | ||
+ | $ perl count_EcoRI_site.pl EcoRI.fasta | ||
+ | Seq_1 has 3 EcoRI sites | ||
+ | Seq_2 has 2 EcoRI sites | ||
+ | </ | ||
+ | See a sample answer [[tutorials: | ||
tutorials/perl_exercises.1280592684.txt.gz · Last modified: 2010/08/01 00:11 by chkuo