#! /usr/bin/perl use strict; #Scott Doniger #January 19, 2005 #ReadTabDelimited.pl #This is a simple program to read in a tab-delimited file (like what you #might get out of Excel) and parse the different columns. #You will hopefully see how file input and output works, and out to use arrays. #To use the program enter perl ./ReadTabDelimited.pl filename # where filename is a tab-delimited file #You might also want to redirect the output using the greater than sign. #perl ./ReadTabDelimited.pl file1 > newfile #check out the contents of file1. if (scalar (@ARGV) != 1){ #the phrase "scalar (@arrayname)" says "treat the array as a scalar", which in this case means #the number of element in the array ARGV. die "You did not give me a file to work with. Please include the filename\n"; } my $tabfile = shift @ARGV; #@ARGV is the array of the input parameters. Shift grabs the first parameter open INPUT, $tabfile; #we're now creating a file handler called INPUT which will allow us to access the lines of $tabfile. #Now we want to read through the file, put each column of the tab-delimited file into an array. #For demonstration sake, we'll print out the first, third, and fifth columns. my $counter = 0; while (my $line = ){ #so $line is now equal to the next line of the file my @data = split "\t", $line; #this line creates an array, @data. Each element of that array is a column of the line. #split takes a delimiter (e.g. tab "\t", space "\s", comma ","), and a string. It returns #each element of that string. print "line $counter = ".$data[0]."\t".$data[2]."\t".$data[4]."\n"; #print the line number, the first, third, and fifth element of the array. $counter++; } close INPUT; #easy, right?