Login

Join for Free!
112379 members
table of contents table of contents

Biology Articles » Bioinformatics » Introductory Workbook on Perl for Biology Students » 4. Perl program for file handling

4. Perl program for file handling
- Introductory Workbook on Perl for Biology Students

This program demonstrates how to read contents from a file. Here we first define a scalar variable with a null value and provide the name of the file in $krupa.seq. If the file is located in the same directory where the Perl program is located then we need not specify the full path, else we need to specify full path of the file.

               

To open the file we use a command called “open”. The “FILE” here is the file handler. The $krupa.seq contains the file name to be opened. The file is read by using the file handler.

 

<FILE> reads the first line in the file and stores it in the variable $dna. Here the whole line is saved as one value as the variable is a scalar. If the variable was an array, all the lines would have been read and each line would have been saved as one value in the array. This <FILE> operation is used in the while loop to read all the lines of the file in a sequence. Once the line is read and stored in $dna, we perform a chomp on the variable to remove the trailing character (enter symbol). We now concatenate the value with the existing value of $longdna and save the whole result again in $longdna. Therefore it should be written as

 

$longdna=$longdna.$dna;

 

To make it short we write it as

$longdna.=$dna;

 

That is, we have reduced it by putting “.=” which means concatenate the two values and save the result to the left hand side variable. The same is with “+=” for addition and other mathematical operations.

 

After every line is read, chomp and concatenated the variable $longdna contains the complete result which is printed on to the screen. The while loop ends when there are no more lines to be read, that is it has reached to the end of the file.

 

  1. #filehandler and nullstring
  2. $longdna='';
  3. $pkrupa.seq="sample.seq";
  4. open(FILE,$krupa.seq);
  5. while($dna=<FILE>){
  6. chomp($dna);
  7. $krupa.seq.=$protein;
  8. }
  9. close FILE;

10.     print"here is the dna:$longdna:\n\n";

  1. exit;

 

 

RESULTS:

 

here is the longdna:TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGCTCCTCGGGTCGATGCGATAGCTAGCTAGATCT :

rating: 3.71 from 112 votes | updated on: 30 Jan 2009 | views: 90685 |

Rate article:







excellent!bad…