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


Jan 14, 2013, 6:46 AM

Views: 2155
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 = ("");


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

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

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

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





foreach $zeile (@ar){


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


Jan 14, 2013, 7:18 AM

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

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

@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:

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

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

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




Jan 14, 2013, 7:49 AM

Views: 2148
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


Jan 14, 2013, 8:05 AM

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

i dont know the meaning of this sentence

If you put

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).


Jan 15, 2013, 6:03 AM

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

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

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


Jan 15, 2013, 11:19 PM

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

Your code should look like this:

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) {

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)

New User

Jan 24, 2013, 8:10 AM

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

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