
Help with indentation
--0015174734307f140604a18b9e68
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
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 =3D 0;
my [at] seq;
my [at] seqheader;
my [at] targets;
my $index =3D 0;
my $index1 =3D -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]");
[at] targets =3D <TARGETS>;
#Just printing the sequences we are looking for.
print "We are looking for:\n";
foreach ( [at] 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=
?
close (TARGETS);
#Here we use the subroutine readFasta to read the FASTA file and store the
names in the array [at] seqheader and the sequences in the array [at] seq.
readFasta($ARGV[1]);
print "\nThere are $seqnum sequences in FASTA file\n";
print "\nWe found:\n\n";
#Here is where we interate the [at] 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?
foreach my $seqheaders( [at] seqheader)
{
$index1 =3D $index1 + 1;
if (grep {$seqheaders =3D~ /$_/} [at] targets)
{
print "$seqheaders\n";
print "$seq[$index1]\n";
$index =3D $index + 1;
}
next;
}
sub readFasta
{
my $line;
my $first;
if ( [at] ARGV =3D=3D 0){
die "No FASTA file specified.\n"
}
open(FILE, "< [at] _[0]");
$seqnum =3D 0;
$first =3D 0;
while (defined($line =3D <FILE>))
{
chomp($line);
if ($line =3D~ /^>/)
{
$seqheader[$seqnum] =3D $line;
$seqnum =3D $seqnum + 1;
if ($first =3D=3D 0)
{
$first =3D 1;
}
next;
}
if ($first =3D=3D 0){
die "Not a standard FASTA file. Stop.\n";
}
$seq[$seqnum - 1] =3D $seq[$seqnum - 1] .$line;
}
close(FILE);
}
--
"Education is not to be used to promote obscurantism." - Theodonius
Dobzhansky.
"Gracias a la vida que me ha dado tanto
Me ha dado el sonido y el abecedario
Con =E9l, las palabras que pienso y declaro
Madre, amigo, hermano
Y luz alumbrando la ruta del alma del que estoy amando
Gracias a la vida que me ha dado tanto
Me ha dado la marcha de mis pies cansados
Con ellos anduve ciudades y charcos
Playas y desiertos, monta=F1as y llanos
Y la casa tuya, tu calle y tu patio"
Violeta Parra - Gracias a la Vida
Tiago S. F. Hori
PhD Candidate - Ocean Science Center-Memorial University of Newfoundland
--0015174734307f140604a18b9e68--
Re: Help with indentation
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 [at] seq;
>my [at] seqheader;
>my [at] 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]");
>
> [at] targets = <TARGETS>;
>
>#Just printing the sequences we are looking for.
>
>print "We are looking for:\n";
>
>foreach ( [at] 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 [at] seqheader and the sequences in the array [at] seq.
>
>readFasta($ARGV[1]);
>
>print "\nThere are $seqnum sequences in FASTA file\n";
>
>print "\nWe found:\n\n";
>
>#Here is where we interate the [at] 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( [at] seqheader)
>{
> $index1 = $index1 + 1;
> if (grep {$seqheaders =~ /$_/} [at] targets)
> {
> print "$seqheaders\n";
> print "$seq[$index1]\n";
> $index = $index + 1;
>
> }
> next;
>}
>
>
>
>sub readFasta
>{
> my $line;
> my $first;
> if ( [at] ARGV == 0){
> die "No FASTA file specified.\n"
> }
> open(FILE, "< [at] _[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-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Help with indentation
>>
>> 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.
>>
> 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.
>
A lot of text editors, especially the command-lines [0], provide a
syntax checker. One of the features of these syntax checkers is their
ability to pair up curly braces. So when you get the dreaded "Missing
braces on..." error you can put your cursor on a end brace and it
shows you were it starts. This can save you a lot of time.
HTH,
Dp.
[0] http://vimdoc.sourceforge.net/htmldoc/syntax.html
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Help with indentation
--0016e6db7bcb8f8c8d04a19bbbfe
Content-Type: text/plain; charset=ISO-8859-1
On Fri, Apr 22, 2011 at 10:08 PM, Tiago Hori <tiago.hori [at] gmail.com> wrote:
> Hi List,
>
> Howdy. Before we start, instead of using wikipedia, if you can get your
hands on it and feel like spending a few hours of your time, Perl Best
Practices[0] is the way to go.
However, assuming that you are a busy person (and, as a PhD student, broke
and can't afford books), here's something that will alleviate most of your
style woes: perltidy[1]. perltidy is a nifty utility that will grab your
code and vomit a "tidied" version. In fact, let's go grab your original post
and run it through perltidy..
http://ideone.com/VAdfe
Which is looking pretty nice already, eh. You can change how it indents too,
so you'll get the braces however you prefer them and sosuch. In any case,
after running a couple of your scripts through perltidy, some of the style
should rub off you and you'll start doing it naturally.
That aside, some corrections to your code..
You are using a bunch of global variables -This is generally a big red flag,
as later on if the program grows or you make a change it might have weird
effects at a distance. For example, all the processing of $seqnum is inside
readFasta, and thenonce outside to print the results. Instead of that, you
might want to declare two versions of $seqnum: One inside of readFasta, and
one outside, to receive the results of that function.
Basically:
my ($seqnum) = readFasta( $ARGV[0] );
sub readFasta {
my $seqnum = 0;
...
return $seqnum;
}
We'll get back to this later.
Then, you are using a lot of bareword filehandles - stuff like TARGETS.
Here's the chance to introduce another nifty utility - perlcritic[2]. You
may recall me mentioning Perl Best Practices before - The book isn't only
about style, but also about avoiding common mistakes in Perl. Bareword
filehandles can occasionally be a big bag of nasty; You'll want a lexical
filehandle instead:
open( my $target, "<$ARGV[0]" );
[at] targets = <$target>;
close $target;
But that's not good enough yet : ) That open may be trouble. You are using
the two-argument version, which is magical. For safety's sake, you'll want
the three-argument version:
open my $target, '<', $ARGV[0];
But we aren't done with open yet. What if the file wasn't found? Your
program will go on merrily, but without a file to read from, that seems
hardly what you'd like it do you. So make it stop:
open my $target, '<', $ARGV[0] or die "File not found: $ARGV[0]: $!";
You could do that, of course, but that's a bunch of extra typing. You could
use the autodie pragma[3] which will do the 'or die's for you.
Beyond that, there's only one big error in what you sent, though. in
readFasta, you do this:
if ( [at] ARGV == 0 ) {
die "No FASTA file specified.\n";
}
But that should be
if ( [at] _ == 0 ) {
die "No FASTA file specified.\n";
}
And then this:
open(FILE, "< [at] _[0]");
Which should probably be
open my $file, '<', $_[0];
A nitpick, but you also have an $index variable that you don't use. You can
probably also let go of that grep and replace it with a smart match:
if ($seqheader[$index] ~~ [at] targets) {
...
Since I'm kind of bored, here's my shot at your program; I've never had to
deal with FASTA files, so I left some stuff in readFasta untouched:
http://ideone.com/kk0OI
Brian.
[0] http://oreilly.com/catalog/9780596001735/
[1] http://search.cpan.org/~shancock/Perl-Tidy-20101217/bin/perl tidy and
http://search.cpan.org/~shancock/Perl-Tidy-20101217/lib/Perl /Tidy.pm
[2] http://search.cpan.org/~elliotjs/Perl-Critic-1.115/bin/perlc ritic
[3] http://perldoc.perl.org/autodie.html
--0016e6db7bcb8f8c8d04a19bbbfe--
Re: Help with indentation
--Apple-Mail-1--1046971920
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain;
charset=us-ascii
Hi Brian and List
>
> http://ideone.com/kk0OI
>
I was studying the references about regular expression you pointed me to on t=
he link above and I just wanted make sure I understand properly. You used th=
e \Q just in case there is a metacharacter in one of the names that gets sto=
red in $_? Could that affect my search? Let's for example that on the name t=
hat goes to $_ at one point I have something like [a-z] would the regular ex=
pression there (/$_/) read that as a range of a to z if i didn't use the \Q.=
Thanks!
T. =
--Apple-Mail-1--1046971920--
Re: Help with indentation
--0016e6dd9634d37f1204a1b1cd7a
Content-Type: text/plain; charset=ISO-8859-1
On Sun, Apr 24, 2011 at 5:42 PM, Tiago Hori <tiago.hori [at] gmail.com> wrote:
> Hi Brian and List
>
>
> http://ideone.com/kk0OI
>
> I was studying the references about regular expression you pointed me to on
> the link above and I just wanted make sure I understand properly. You used
> the \Q just in case there is a metacharacter in one of the names that gets
> stored in $_? Could that affect my search? Let's for example that on the
> name that goes to $_ at one point I have something like [a-z] would the
> regular expression there (/$_/) read that as a range of a to z if i didn't
> use the \Q.
>
> Thanks!
>
> T.
>
Hey Tiago.
That's exactly right. For example, see:
http://ideone.com/YGq85
I'm not sure if it would affect your search. I dimly recall that FASTA files
are just protein sequences, in which case it shouldn't really matter. "Just
being paranoid, carry on" :P
On the other hand, if you had something like, say, '3.4', then that dot
would be a metacharacter unless you used \Q or quotemeta - So it would match
3.4, but also 344, or 3a4 and so on.
My rule of thumb is that, if you didn't compile it using qr//, you probably
want the quotemeta version.
Brian.
--0016e6dd9634d37f1204a1b1cd7a--
Re: Help with indentation
>>>>> "BF" == Brian Fraser <fraserbn [at] gmail.com> writes:
BF> http://ideone.com/YGq85
a small request. you can post link to pastebots but also paste the code
in emails here. i can reply and quote code better staying in my emailer
than going back and forth to a web page. emacs does a fine job of
marking off quoted emails which i don't get when using a pastebot.
<was that polite enough for the tone phreaks out there?> :)
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Help with indentation
--0015175cd8d27909fe04a1b59809
Content-Type: text/plain; charset=ISO-8859-1
On Sun, Apr 24, 2011 at 11:18 PM, Uri Guttman <uri [at] stemsystems.com> wrote:
> a small request. you can post link to pastebots but also paste the code
> in emails here. i can reply and quote code better staying in my emailer
> than going back and forth to a web page. emacs does a fine job of
> marking off quoted emails which i don't get when using a pastebot.
>
>
Sure, my bad. I got tired of GMail screwing up my indentation, but hadn't
considered that others might not be so impaired.
Here's the code for future reference:
use 5.010;
my $string = 'Some string';
my $string_regex = '([A-Za-z])';
my $as_regex = qr/$string_regex/;
my $quoted_regex = qr/\Q$string_regex/;
say "Without \\Q, against [$string], [$string_regex] is interpreted as
[$as_regex] and ", match_against($string, $as_regex);
say "Without \\Q, against [$string_regex], [$string_regex] is interpreted as
[$as_regex] and ", match_against($string_regex, $as_regex);
say "";
say "With \\Q, against [$string], [$string_regex] is taken literally as
[$quoted_regex] and ", match_against($string, $quoted_regex);
say "With \\Q, against [$string_regex], [$string_regex] is taken literally
as [$quoted_regex] and ", match_against($string_regex, $quoted_regex);
$as_regex = qr/(\Q$string_regex\E)/;
say "\nBonus: Against [$string_regex], [(\\Q$string_regex\\E)] is
interpreted as [$as_regex] and ", match_against($string_regex, $as_regex);
sub match_against {
my ($string, $regex) = [at] _;
return $string =~ $regex ? "matched, with \$1 => [$1]" : "didn't match.";
}
<was that polite enough for the tone phreaks out there?> :)
Just in case, drop the caps everywhere! :P
Brian.
--0015175cd8d27909fe04a1b59809--
Re: Help with indentation
Hi Uri,
On Monday 25 Apr 2011 05:18:21 Uri Guttman wrote:
> >>>>> "BF" == Brian Fraser <fraserbn [at] gmail.com> writes:
> BF> http://ideone.com/YGq85
>
> a small request. you can post link to pastebots but also paste the code
> in emails here. i can reply and quote code better staying in my emailer
> than going back and forth to a web page. emacs does a fine job of
> marking off quoted emails which i don't get when using a pastebot.
>
> <was that polite enough for the tone phreaks out there?> :)
>
I guess it was for a change. :-) UriGuttman++ . It was a valid and wise
request, naturally, too, from the education for usability persepective (even
for people who are not using Emacs or a similar client), so thanks!
I hope you keep up this trend.
Sorry if I sounded too phony, but I tried to be sincere,
Shlomi Fish (E-mail ethics policeman - ;-))
--
------------------------------------------------------------ -----
Shlomi Fish http://www.shlomifish.org/
"The Human Hacking Field Guide" - http://shlom.in/hhfg
Tcl is Lisp on drugs. Using strings instead of S-expressions for closures is
Evil with one of those gigantic E's you can find at the beginning of chapters.
Please reply to list if it's a mailing list post - http://shlom.in/reply .
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/