CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



johnny241088
Novice

Jan 14, 2013, 6:46 AM

Post #1 of 7 (1040 views)
Problems at searching and counting Can't Post

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

Post #2 of 7 (1036 views)
Re: [johnny241088] Problems at searching and counting [In reply to] Can't Post

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

Post #3 of 7 (1033 views)
Re: [rovf] Problems at searching and counting [In reply to] Can't Post

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

Post #4 of 7 (1030 views)
Re: [johnny241088] Problems at searching and counting [In reply to] Can't Post


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

Post #5 of 7 (1021 views)
Re: [johnny241088] Problems at searching and counting [In reply to] Can't Post

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

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


7stud
Enthusiast

Jan 15, 2013, 11:19 PM

Post #6 of 7 (1011 views)
Re: [johnny241088] Problems at searching and counting [In reply to] Can't Post

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

Post #7 of 7 (972 views)
Re: [7stud] Problems at searching and counting [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives