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”.
- #file handler
- $DNA="sample.seq";
- open(dna1,$DNA);
- $DNA2=<dna1>;
- close dna1;
- print"the sequence of
krupa.seq:$DNA2\n";
- #file handler for two strings
- open(dna1,$DNA);#removed the above line
- @DNA2=<dna1>;
- close dna1;
- print"the sequence of two strings
of krupa.seq:@DNA2\n";
- #chomp the two strings
- chomp (@DNA2);
- print"the results of chomp
DNA:@DNA2\n";
- #join the two strings
- $DNA3=join('',@DNA2);
- print"the result of join of two
strings:\n$DNA3\n";
- #length of DNA sequence
- $length=length$DNA3;
- 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