Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, Here is my code written to identify the particular position which is after a string (chr*).
my input file looks some thing like this
aanbb:anhn:iuopl:12345 chr1 12345 asnmkol * # kjiiii.....anmkij:lpolk: +lopll:abnnj chr5 123222 polko * dddfgg .... aaanbb:anhn:iuopl:aanjuj chr2 44345 asnmkol * # kjiiii.....anmkij:lpol +k:lopll:abnnj chr7 567222 polko * dddfgg .
i wanted to get the @pos as the immediate occurance after chr* as there are 2 such occurances in each line the first occrance has to be stored in pos[0] and second in pos1 for pos[0] pattern search has to be made wih in pos[0]+6 characters and same vs for pos1 but backwards
use strict; use warnings; open (my $fhConditions, "<1.txt"); my $l=6; open (my $read, "<2.txt"); my @e = <$read>; my $d = join('', @e ); $d =~ s/\s+//g; while (my $line = <$fhConditions>) { chomp $line; my @pos = $line[$i] =~/chr*\s+(.+?)\s/g; if($pos[0] =/[0-9]/) { my $match = substr($d,$pos[0],$l) print "$line" if $match =~m/AAGCTT/; } if($pos[1] =/[0-9]/) { my $a = $pos[0]-$l; my $match = substr($d,$a,$l); print "$line" if $match =~m/AAGCTT/; } }
ERRORS
Global symbol "@line" requires explicit package name at restriction.pl + line 11. Global symbol "$i" requires explicit package name at restriction.pl li +ne 11. syntax error at restriction.pl line 15, near ") print" Global symbol "$match" requires explicit package name at restriction.p +l line 15.

Replies are listed 'Best First'.
Re: How to get rid of the errors in my perl script
by davido (Cardinal) on Aug 01, 2012 at 07:16 UTC

    $line is a scalar variable that you declare in line 9. $line[$i] is an element of an array named @line, which is a totally different (undeclared) variable that has no relationship to the scalar named $line.

    The scalar variable $i makes its first appearance in your script in line 11. What value is it supposed to contain? If you don't declare it, and don't assign it a value, it's probably not going to do what you need it to do.

    You're missing a semicolon after the substr(...) call on line 14. (It's usually a good idea to look at the line right above where syntax errors are reported.)

    Finally: Install perltidy and pass your script through it. Judicious use of white-space makes code more readable, and errors easier to detect.

    These only address the superficial issues: the error messages you're seeing. Once you fix those issues, it becomes time to debug the logic.


    Dave

      open (my $fhConditions, "<a.txt"); my $l=6; open (my $read, "<b.txt"); my @e = <$read>; my $d = join('', @e ); $d =~ s/\s+//g; while (<$fhConditions>) { push (my @line, $_); $count++; } for ($i = 0; $i < $count; $i++) { my @pos = my $line[$i] =~ /chr*\s+(.+?)\s/g; if($pos[0] =~/[0-9]/) { my $match = substr($d,$pos[0],$l); print "$line[$i]" if $match =~m/AAGCTT/; } if($pos[1] =~/[0-9]/) { my $a = $pos[0]-$l; my $match = substr($d,$a,$l); print "$line[$i]" if $match =~m/AAGCTT/; } }
      this the code modified after the suggestions. but still i am getting some error
      yntax error at restriction.pl line 13, near "$line[" Execution of restriction.pl aborted due to compilation errors.

        You should remove my from line 13. It doesn't belong there and is causing this error.

Re: How to get rid of the errors in my perl script
by Ratazong (Monsignor) on Aug 01, 2012 at 07:19 UTC

    Hi!

    To get rid of the errors, read the error-messages.

    • line 11: you use $line[$i], but you have defined $line as a scalar (not an array)
    • line 11: you use $i, but have never defined it ... (hint: and you don't need it)
    • line 15: a syntax error is often caused by a missing semicolon the line before
    • line 15: often error-messages are created due to errors before ... so solve the first three errors and check if this one prevails
    HTH, Rata
Re: How to get rid of the errors in my perl script
by Athanasius (Archbishop) on Aug 01, 2012 at 07:25 UTC

    In addition to the points made above by davido and Ratazong, the line:

    if($pos[0] =/[0-9]/)

    should be written as:

    if ($pos[0] =~ /[0-9]/)

    Likewise for the line:

    if($pos[1] =/[0-9]/)

    HTH,

    Athanasius <°(((><contra mundum

      i have made all the possible corrections. but still i fail to get the proper output.
      open (my $fhConditions, "<1.txt"); my $l=6; open (my $read, "<2.txt"); my @e = <$read>; my $d = join('', @e ); $d =~ s/\s+//g; while (<$fhConditions>) { push (my @line, $_); my $count++; } for (my $i = 0; $i <$count; $i++) { my @pos = $line[$i] =~ /chr[0-9]\s+(.+?)\s/g; if($pos[0] =~/[0-9]/) { my $match = substr($d,$pos[0],$l); print "$line[$i]" if $match =~m/AAGCTT/; } if($pos[1] =~/[0-9]/) { my $a = $pos[0]-$l; my $match = substr($d,$a,$l); print "$line[$i]" if $match =~m/AAGCTT/; } }
      i doubt first of all its not getting the values it self from the array. i am not getting errors as well as output
        i am not getting errors as well as output

        That’s because you’ve removed

        use strict; use warnings;

        from the head of your script, although they were there in the original. With use strict restored, the errors return:

        Global symbol "$count" requires explicit package name at test.pl line +13. Global symbol "@line" requires explicit package name at test.pl line 1 +5. Global symbol "@line" requires explicit package name at test.pl line 1 +9. Global symbol "@line" requires explicit package name at test.pl line 2 +5. test.pl had compilation errors.

        You need to declare my $count and my @line before the while loop, so that they will still be visible in the following for loop:

        ... my (@line, $count); while (<$fhConditions>) { push @line, $_; $count++; } for (my $i = 0; $i < $count; $i++) { my @pos = $line[$i] =~ /chr[0-9]\s+(.+?)\s/g; ...

        See the section “Scope” in the “Functions” chapter of chromatic’s Modern Perl, available free online at http://modernperlbooks.com/books/modern_perl/.

        Note: In your original question, you gave a sample of the contents of your first input file (1.txt), but not of the second (2.txt), so it’s difficult to see what your script is trying to do.

        Athanasius <°(((><contra mundum