undergradguy has asked for the wisdom of the Perl Monks concerning the following question:
I have taken a look at the files and suggestions posted to my previous thread and here is what I made. However it returns a syntax error at line 14 and I can not see why. Does anyone have a suggestion as to why it is returning a syntax error?
#!/usr/bin/perl -w use strict; use diagnostics; my $file = "$ARGV[0]"; open (DAT, $file) || die("Can not open file!"); while (<DAT>) { my $seq = $_; if($seq =~m/^[ATCG]+$/){ my $DNA = ''; $DNA = $seq; my $revcom = reverse($DNA); $revcom =~ tr/ACGTacgt/TGCAtgca/; print $revcom; };
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Isolating DNA cont.
by toolic (Bishop) on Dec 20, 2018 at 20:41 UTC | |
I also got rid of the trailing semicolon after your right curly. | [reply] [d/l] [select] |
by undergradguy (Novice) on Dec 20, 2018 at 21:03 UTC | |
| [reply] |
Re: Isolating DNA cont.
by AnomalousMonk (Archbishop) on Dec 20, 2018 at 23:41 UTC | |
Your OPed question has been well and truly answered. Here are some general comments on your posted code. In sum, I might have written the program as follows. Again, many of the programming choices represent personal preferences; take them as you find them. (See autodie to get rid of all the explicit ... or die "...: $!"; expressions.) Data file dna.dat:
Output: The next step: Put the reverse complement functions into a .pm module with an accompanying Test::More .t file. :) Update 1: It's true that -w on the command line enables warnings globally (see $^W special variable in perlvar). However, -Mstrict on the command line still only has lexical scope, in this case the scope of the code in the -e "..." switch. Given a module Unstrict.pm consider
Give a man a fish: <%-{-{-{-< | [reply] [d/l] [select] |
by undergradguy (Novice) on Dec 21, 2018 at 20:08 UTC | |
| [reply] |
Re: Isolating DNA cont.
by talexb (Chancellor) on Dec 21, 2018 at 15:51 UTC | |
A general comment on developing software that works for me (and I suggest it whenever I can), is to always create balanced operations or braces, then go back and fill in the middle. So you would start with your boilerplate opening: You would have collected your input, opened and closed the file: Then you would have added the skeleton of your while loop: Even stopping there, you'd have something that opened a file, read through each record, and closed it. You don't have to remember to finish the loop later, or think about closing the file .. you've already done that, and you can then go back and put the meat into the sandwich. Adding the if statement (without a condition) brings us to this: To me, that's way easier than writing the opening part of a brace or an operation, then later, trying to remember what I have left to go back and clean up. | [reply] [d/l] [select] |
by kcott (Archbishop) on Dec 22, 2018 at 11:29 UTC | |
G'day Alex, ++ I just wanted to add my agreement with, and recommendation of, this method of coding. I started doing this many years ago — in fact, possibly a couple of decades ago — and it's made my (programming) life a whole lot easier. Nowadays, without really having to think about it, I start sections of code like:
or
I then don't have to remember to add closing braces. I also don't need to worry about aligning the closing braces to get the proper indentation: auto-indent has already done that for me. The same works for other balanced characters. Building up a complex expression incrementally like this:
almost never results in a "mis-matched parentheses" type error. Attempting to type that final expression all at once, often will; and leaves you having to scan back and forth over the expression, counting opening and closing parentheses. I also use this method for building up complex regular expressions (using the x or xx modifiers). This may not work for everyone; for example, perhaps syntax-highlighting is set up to indicate mis-match problems. However, it certainly works for me and clearly works for you. I would recommend people at least try it. — Ken | [reply] [d/l] [select] |
by hippo (Archbishop) on Dec 22, 2018 at 11:47 UTC | |
Nowadays, without really having to think about it, I start sections of code like I literally don't have to think about it because my editor does it for me. It is worth the investment of getting to know your editor of choice so that such boilerplate can be automated away. | [reply] |
by talexb (Chancellor) on Dec 22, 2018 at 21:15 UTC | |
by kcott (Archbishop) on Dec 22, 2018 at 12:25 UTC |