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.
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.
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
- close dna1;
- print"the sequence of
- #file handler for two strings
- open(dna1,$DNA);#removed the above line
- close dna1;
- print"the sequence of two strings
- #chomp the two strings
- chomp (@DNA2);
- print"the results of chomp
- #join the two strings
- print"the result of join of two
- #length of DNA sequence
- print"the length of two strings of
the sequence of
the sequence of two strings
the results of chomp
the result of join of two
the length of two strings of