At 10:38 PM -0230 4/22/11, Tiago Hori wrote:
Hi List,

I have been trying to follow one of code indentation guidelines from
wikipedia, but I have some questions that I can find answers, I would
appreciate if someone could help me or point me towards better references.


So here is something I have been working on:

-------------------------------Code------------------------------------------------

use strict;

my $seqnum = 0;
my @seq;
my @seqheader;
my @targets;
my $index = 0;
my $index1 = -1;

#Here we open the filehandle TARGETS to connect to the file containing the
names of the sequences we want to find.

open(TARGETS, "<$ARGV[0]");

@targets = <TARGETS>;

#Just printing the sequences we are looking for.

print "We are looking for:\n";

foreach (@targets)
{
    print "\n$_";
}

As I understand I am supposed to align the start curly brace opening the
loop with the one closing, so I did it here. My first question is: should I
everything from now on be indented forward or should be aligned to the left?

No. The indentation should provide a visual clue as to where the loop begins and ends. Once the loop has ended, the indentation should return to what it was at the start of the loop.


close (TARGETS);

#Here we use the subroutine readFasta to read the FASTA file and store the
names in the array @seqheader and the sequences in the array @seq.

readFasta($ARGV[1]);

print "\nThere are $seqnum sequences in FASTA file\n";

print "\nWe found:\n\n";

#Here is where we interate the @seqheader array looking for the names we
wanted

The second question is the same as the first really: when I start a second
loop, should I indent it even further to the right?

Yes, although below you are showing an if statement, not a loop. However, the rule should apply to any block of code. The body of the block should be indented one "unit" more than its enclosing block. A unit is generally a tab or some small number of spaces. Some frown on tab characters. Some like 4 spaces. I prefer 2 spaces. Just be consistent.


foreach my $seqheaders(@seqheader)
{
    $index1 = $index1 + 1;
    if (grep {$seqheaders =~ /$_/} @targets)
      {
         print "$seqheaders\n";
         print "$seq[$index1]\n";
         $index = $index + 1;

      }
    next;
}



sub readFasta
{
    my $line;
    my $first;
    if (@ARGV == 0){
      die "No FASTA file specified.\n"
    }
    open(FILE, "<@_[0]");
    $seqnum = 0;
    $first = 0;
    while (defined($line = <FILE>))
    {
        chomp($line);
        if ($line =~ /^>/)
        {
          $seqheader[$seqnum] = $line;
          $seqnum = $seqnum + 1;
          if ($first == 0)
          {
                $first = 1;
          }
            next;


The above line should be at the same indenting level as the if statement 4 lines previous.

        }
        if ($first == 0){

Here you have the opening brace on the same line as the if that starts a block. Elsewhere, you put the opening brace on its own line. Both are OK, but it is better to be consistent.


                  die "Not a standard FASTA file. Stop.\n";
                         }

The closing brace above should not be indented so much.


            $seq[$seqnum - 1] = $seq[$seqnum - 1] .$line;
    }
    close(FILE);
}

I would say your indenting looks fine with some minor exceptions as noted (some of which might be caused by my email program).


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to