next up previous
Next: Subroutines (functions) Up: perl1st Previous: Flow control

Subsections

File I/O

open()

open(INFILE, "in_fileName.txt") || die("Can't open in_fileName.txt");
...
close (INFILE)

Accessing the file content

Line by line processing.

while(<INFILE>) {  # assign each line to $_
  chomp ($_);
  print "$.: $_\n";
}
This is a very common way of processing data.

Command line arguments: @ARGV

Example:

#!/usr/bin/perl -w
# getCol.pl colNum file
# Extract one column from a tab-delimited file.
# The program takes two arguments:
#   1. The column number to extract
#   2. Name of file

my ($colNum,  $fileName) = @ARGV;

open IN, "< $fileName" || die "Can't open $fileName";

while(<IN>) {
  chomp;
  my @line = split(/\t/, $_);  # split tab delimited line ($_) and make an array

  print $line[$colNum - 1], "\n";
}

close(IN);
exit;

Printing out

Exercises

  1. Create a perl script, fastaNames.pl, which takes a filename (of a file in FASTA format) as the argument, and print out all sequence names in this file. Let's say you have a FASTA file ( animals.fasta) with following contents:

    >llama
    GGTCTTATATTTTATATT
    >camel
    ATATGATC
    GACTTTAC
    CCCTCTT
    >owl
    TTTTCTGAT
    TTGCTG

    Then

    ./fastaNames.pl animals.fasta

    should print out:

    >llama
    >camel
    >owl


next up previous
Next: Subroutines (functions) Up: perl1st Previous: Flow control
Naoki Takebayashi 2011-10-06