in reply to Parsing Multiple Alignment -- using Perl for DNA
Your code is an perfect example of what is often called "baby Perl", i.e. code using only a small subset of the language. There is nothing derogative in this expression, quite to the contrary: beginners are encouraged to write scripts in baby Perl, this is the right way to start learning. And most monks here (including myself), perhaps all monks, have started this way.
I would like to offer a few possibly more "Perlish" ways of doing the same thing, but without trying to do too clever things:
You may want to use a data structure rather than 8 variables. The eight code lines above can be replaced by:@input = <INPUT>; $query1_name = $input[0]; $query1 = $input[1]; $query2_name = $input[2]; # ... $query4 = $input[7];
Which, for an @input array containing numbers fro 1 to 8, gives me the following data structure:my @query; ($query[$_]{name}, $query[$_]{content}) = @input[2*$_, 2*$_ + 1] for 0 +..3;
This structure is called an array of hashes (AoH): it is an array of four elements, in which each element is a reference to a hash with two keys.0 ARRAY(0x600500b60) 0 HASH(0x600500a88) 'content' => 2 'name' => 1 1 HASH(0x6005cffa8) 'content' => 4 'name' => 3 2 HASH(0x6005cffd8) 'content' => 6 'name' => 5 3 HASH(0x600635958) 'content' => 8 'name' => 7
Similarly, you can change:
to this:@query1 = split ('', $query1); @query2 = split ('', $query2); @query3 = split ('', $query3); @query4 = split ('', $query4);
and for the lengths:$query[$_]{split_content} = [ split '', $query[$_]{content} ] for 0..3 +;
although I am not sure this is needed since you seem to be using only $length1, but this may be another bug. Now the data structure is looking like this:* $query[$_]{length} = scalar @{ $query[$_]{'split_content'}} for 0. +.3;
This:0 ARRAY(0x600500b60) 0 HASH(0x6005fdbb8) 'content' => 2 'length' => 1 'name' => 1 'split_content' => ARRAY(0x600635d30) 0 2 1 HASH(0x6005d0050) 'content' => 4 'length' => 1 'name' => 3 'split_content' => ARRAY(0x60063d850) 0 4 2 HASH(0x60063d928) 'content' => 6 'length' => 1 'name' => 5 'split_content' => ARRAY(0x6006360a8) 0 6 3 HASH(0x6006359d0) 'content' => 8 'length' => 1 'name' => 7 'split_content' => ARRAY(0x600636060) 0 8
might be better written:while ($counter <= $length1) #...
which will do for you the incrementation missing in your code.for my $counter (0..$length1) { ...
I won't go further into your code, but I would add that you should should take the habit of declaring all your variables (with the my operator) and to insert the following pragmas at the top of your programs:
use strict; use warnings;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Parsing Multiple Alignment -- using Perl for DNA
by le_albatross (Initiate) on May 15, 2015 at 19:43 UTC |