Login

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

Biology Articles » Bioinformatics » Introductory Workbook on Perl for Biology Students » 5. Perl Program for File handling For Scalar And Array Variables

5. Perl Program for File handling For Scalar And Array Variables
- Introductory Workbook on Perl for Biology Students

This program shows the difference in results in using scalar variables and array variables in retrieving data from a file. We first assign a file name to a scalar variable ($DNA) for our program. We then use the open command to open the file. The command <dna1> reads the first line of the file and stores the value in the scalar variable $DNA2. A scalar variable can hold only one value at a time and hence the next lines of the file are not stored in the variable instead it skips to the next line where the close command is issued. So the variable $DNA2 contains only the first line of the file.

 

The next test is to use an array. In line 9, the first line of the file is read and stored in the array variable @DNA2. Since the variable is an array all the lines from the file can be read and stored into this array. Each line would be stored as one value of the array. Once the file is read completely the program goes to the next line where the file is closed.

 

We would now like to join all the values of the array $DNA2 into one complete string. For that we would first need to remove the “enter” character at the end of each value of the array. The enter character appears each time we hit the enter key on our keyboard while writing to a file. This enter character is invisible. As all the lines are copied into the array, the values also contain this enter character. So we first remove this character by using chomp on the array. Now the array is free of the enter character. We will now join the values of the array by using a command called “join”. The values of the array are joined based on ‘null’ (‘’ without any space between the quotes) as quoted in line 16.  Hence there would be no value add between the values of the array. We now get one complete string from the values of the array and are stored in $DNA3. We find out the length of this variable by using a command called “length”.

 

  1. #file handler
  2. $DNA="sample.seq";
  3. open(dna1,$DNA);
  4. $DNA2=<dna1>;
  5. close dna1;
  6. print"the sequence of krupa.seq:$DNA2\n";
  7. #file handler for two strings
  8. open(dna1,$DNA);#removed the above line
  9. @DNA2=<dna1>;
  10. close dna1;
  11. print"the sequence of two strings of krupa.seq:@DNA2\n";
  12. #chomp the two strings
  13. chomp (@DNA2);
  14. print"the results of chomp DNA:@DNA2\n";
  15. #join the two strings
  16. $DNA3=join('',@DNA2);
  17. print"the result of join of two strings:\n$DNA3\n";
  18. #length of DNA sequence
  19. $length=length$DNA3;
  20. print"the length of two strings of DNA=$length\n";

 

RESULTS:

the sequence of krupa.seq:TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGC

 

the sequence of two strings of krupa.seq:TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGC

 TCCTCGGGTCGATGCGATAGCTAGCTAGATCT

 

the results of chomp DNA:TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGC TCCTCGGGTCGATGCGATAGCTAGCTAGATCT

the result of join of two strings:

TACTGTGGCCGTGCGTGGCTGCCGTTGCGCTGCTCCTCGGGTCGATGCGATAGCTAGCTAGATCT

the length of two strings of DNA=66


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

Rate article:







excellent!bad…