Reputation: 31
I have a text file that looks like this http://www.uniprot.org/uniprot/?sort=score&desc=&compress=no&query=id:P01375%20OR%20id:P04626%20OR%20id:P08238%20OR%20id:P06213&format=txt
I have to print only the lines in the text file that start with specific pattern (ID).
I tried it this way but it doesn't working:
open (IDS, 'example.txt') or die "Cannot open";
my @ids = <IDS>;
close IDS;
my @IDS= "ID";
foreach my $ids (@ids) {
if (my @ids =~ my @IDS){
print $ids;
}
}
There probably must be some problem in this line **if (my @ids =~ my @IDS){.
If somebody can help me I would be very thankful.
Best
Upvotes: 0
Views: 1392
Reputation: 374
After long list list of comments, here you go! This snippet works for both of your cases!
use strict;
use warnings;
open ( my $input, '<', 'printLinesStartingWithID.txt' ) or die $!;
while ( <$input> ) {
if(/^ID/)
{
print "Matched line starting with ID: $_";
}
if(/^AC/)
{
my ($secondCol) = $_ =~ /^AC...(.*?)\;/; #The three dots is to chop off the three empty spaces followed by 'AC'.
print "Matched line starting with AC. The second column of the line is: $secondCol \n";
}
}
close ( $input );
Upvotes: -1
Reputation: 53478
Your problem is almost certainly this line:
if (my @ids =~ my @IDS){
because my
is declaring a new variable, that 'hides' the one in the parent scope. Applying a regex match when both source and target are arrays is also going to behave in an odd sort of way - you're iterating @ids
one element at a time, but then you're just matching the whole thing. And you're matching against another array, which ... actually works, but only because you're relying on casting the array to a string and back again.
It's also pretty bad style to have upper case and lower case variables of the same name, and you're doing it 3 times with @ids
, @IDS
, and IDS
.
I'm also really not sure that my @IDS = "ID";
is doing what you think it's doing, and neither is trying to treat @IDS
as a pattern in the first place.
Also - reading a file into an array then iterating it is less efficient that just iterating the file line by line.
Your code could probably be simplified as:
open ( my $input, '<', 'example.txt' ) or die $!;
while ( <$input> ) {
print if m/ID/;
}
close ( $input );
Upvotes: 2