Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^3: matchin a pattern 250 characters up and down stream

by muba (Priest)
on Jun 28, 2012 at 23:34 UTC ( #979023=note: print w/ replies, xml ) Need Help??


in reply to Re^2: matchin a pattern 250 characters up and down stream
in thread matchin a pattern 250 characters up and down stream

That's not what it says for me. For me it says,

String found where operator expected at G:\x.pl line 15, near ""Y" "\n +"" (Missing operator before "\n"?) String found where operator expected at G:\x.pl line 17, near ""N" "\n +"" (Missing operator before "\n"?) Global symbol "$AAGCTT" requires explicit package name at G:\x.pl line + 14. syntax error at G:\x.pl line 15, near ""Y" "\n"" Execution of G:\x.pl aborted due to compilation errors.
Which sounds about right. Let's go through those errors one by one, shall we?

String found where operator expected at G:\x.pl line 15, near ""Y" "\n"" (Missing operator before "\n"?)

Look at that. Perl is even being so helpful as to point out what's wrong with that line. The missing operator it's talking about, would be the comma operator. But why wouldn't you just write that as print "Y\n";?

The next error message? I think you can figure that one out.

Global symbol "$AAGCTT" requires explicit package name at G:\x.pl line 14.

Yep. That regex is never going to work. In regular expressions, $ means "end of string". Surely nothing can appear after the end of a string, right? So Perl's being so liberal as to guess what you meant there, and it thinks you want to match against some variable $AAGCTT. This is why I have asked you whether you want to match against a variable or against a literal string. Turns out neither was the case and you meant ^ there all along, which means "beginning of string". Which would work.

We asked for examples, so that we could understand your problem better. In an attempt to simplify your problem, you kept coming up with contrived sample data like abcdefgabcdefgabcdefg, but the monks here aren't afraid of a little DNA. You could've told us -or shown us, preferably- that you were dealing with data in which you needed to find a specific DNA sequence at a specific location.

Anyway, I digress. The question we should be asking ourselves here is, "do you really need regular expressions?" Well, no. Because i's not really like you're pattern matching. Again, we could've caught this much sooner if you had just answered my question: "yes, muba, I do want to check if a literal string, "AAGCTT" to be precisely, appears at the calculated position in file 2." And I would've told you,

"Well, AnonMonk. That's fairly easy. How about this?

my $substring = "AAGCTT"; read $fhCharacters, $data, length($substring); if ( $data eq $substring) ) {
If you run into any problems, let us know! Good luck."

syntax error at G:\x.pl line 15, near ""Y" "\n""

Perl's being so kind as to point to that line again, in case we missed it the first time around or anything. At the same time, it's not pointing out several other things wrong with your code, so let me do that for you.

You should close your if block with a } before the else keyword, and similarily you should open your else block with a { after that keyword.

From a readability point of view, the indentation of the else keyword is one level too deep. And while we're discussing indentation levels anyway, the my ($strand, $chr, $position)... line should go one level deeper. These are just cosmetic issues, of course, but in the long run you really profit from picking up the good habits right from the start.

Speaking of good habits, your script should begin with the lines use strict; and use warnings;. You might want to move your top line down two lines.

Apply these fixes, and let us know how that works out for you.


Comment on Re^3: matchin a pattern 250 characters up and down stream
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979023]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2014-08-02 03:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (54 votes), past polls