Home: Perl Programming Help: Beginner:
Problems at searching and counting



johnny241088
Novice

Jan 14, 2013, 6:46 AM


Views: 2078
Problems at searching and counting

Heyho guys i have some trouble with my written script...

i have a textfile and it looks like this:

Chr1 phytozome8_0 exon 3631 3913 . + . ID=PAC:19656964.exon.1;Parent=PAC:19656964;pacid=19656964
Chr1 phytozome8_0 exon 3996 4276 . + . ID=PAC:19656964.exon.2;Parent=PAC:19656964;pacid=19656964
Chr1 phytozome8_0 exon 3631 3913 . + . ID=PAC:19656964.exon.3;Parent=PAC:19656964;pacid=19656964

and now i should extract the numbers after the word exon for each line,

here my code:

@ar = ("");

$infile="test";

open (IN, "<", "$infile") ||

die "Cannot open file $infile: $!\n";



open (OUT, ">", "exontwice") ||

die "Cannot open file $outfile: $!\n";





while(<IN>)

{

push(@ar,$_);

}



foreach $zeile (@ar){

$startstop=substr($zeile,22,4);

print "$startstop\n";



and now i try to print out the lines if $startstop (the numbers) are for two times in the array... and this is my try but i doesnt work can u help me???

rest of the code:

foreach $line (@ar){
$count = () = @ar =~ /\Q$startstop\E/g;
if ($count==2) {
print OUT "$line";
}

}



}


regards jOhnny


rovf
Veteran

Jan 14, 2013, 7:18 AM


Views: 2074
Re: [johnny241088] Problems at searching and counting

Aside from the fact that many parts in this code are unnecessarily complicated, the bug is here:


Code
@ar =~ /\Q$startstop\E/g;


The pattern match operator forces the left argument into scalar context, so if your file had, for instance 55 lines, you would have here:


Code
55 =~ /\Q$startstop\E/g;


No wonder that this doesn't match. You need to match within $line, i.e.:


Code
$line =~ /\Q$startstop\E/g;  

Code



      
    


johnny241088
Novice

Jan 14, 2013, 7:49 AM


Views: 2071
Re: [rovf] Problems at searching and counting

ah ok i see thx mate, but now i have another problem if i try to run the script i become this message:

Use of uninitialized value $startstop in quotemeta at testskript.pl line 42, <IN> line 10.

and at the moment i dont know the meaning of this sentence, can u help me again?

regards jOhnny


rovf
Veteran

Jan 14, 2013, 8:05 AM


Views: 2068
Re: [johnny241088] Problems at searching and counting


Quote
i dont know the meaning of this sentence


If you put


Code
use diagnostics;


at the top of your code, these messages will be output more verbosly. Though this becomes annoying, when you get more experience, it is very helpful for a beginner.

The meaning of the message means, that you are trying to use the contents of the variable $startstop, but $startstop does not have a value (in other words: It is contains the special 'value' undef).


Rahul6990
Novice

Jan 15, 2013, 6:03 AM


Views: 2059
Re: [johnny241088] Problems at searching and counting

Replace :
$count = () = $line =~ /\Q$startstop\E/g)

With :
$count++ if($line =~ /\Q$startstop\E/g);


7stud
Enthusiast

Jan 15, 2013, 11:19 PM


Views: 2049
Re: [johnny241088] Problems at searching and counting

Your code should look like this:


Code
use strict;  
use warnings;
use 5.012;

open my $INFILE, "<", 'data.txt'
or die "Couldn't open data.txt: $!";

my %results;

while (my $line = <$INFILE>) {
if ($line =~ /exon \s* (\d{4}) /xms) {
push @{$results{$1}}, $line;
}
}

while ( my($key, $array_ref) = each %results) {
my @arr = @{$array_ref};

if (@arr > 1) {
for (@arr) {
print;
}
}
}

However, that program is way too complicated for you at this point. You have not learned the basics of modern perl programming yet. You need to read a beginning perl book before proceeding any further. There are no short cuts when learning computer programming. Whatever you are currently using to learn perl, you should abandon.


(This post was edited by 7stud on Jan 15, 2013, 11:31 PM)


ingebolk
New User

Jan 24, 2013, 8:10 AM


Views: 2010
Re: [7stud] Problems at searching and counting

Thanx 7stud, that's what i was looking for :)