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

Hi monkers, i humbly seek your advice for a syntax error that im not able to spot in my code for hours.
#!/usr/bin/perl use 5.010; use strict; use warnings; open BLOCKS,'blocks.txt'; open START,'start.txt'; open STOP,'stop.txt'; open NP,'np.txt'; open TRIPLET,'>triplet.txt'; my @blocks=<BLOCKS>; my @start=<START>; my @stop=<STOP>; my @np=<NP>; chomp @blocks; chomp @start; chomp @stop; chomp @np; my $i; my $index=0; my $first; my $second; my $third; my $a=0; my $b=1; my $c=3; my $flag; for($i=0;$i<(scalar(@blocks));$i++) { if($blocks[$index]=~m/^\d.+/) { if ($blocks[$a]=~m/^\d.+/ and $blocks[$b]=~m/^\d.+/ and $block +s[$c]=~m/^\d.+/) { if($np[$a] eq $np[$b] || $np[$a] eq $np[$b] || $np[$b] eq +$np[$c]) { if ($np[$a] eq $np[$b]) { $flag=$np[$a]; print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$fl +ag\n"; print TRIPLET "$start[$b]\t$stop[$b]\t$np[$b]\t$fl +ag\n"; print TRIPLET "$start[$c]\t$stop[$c]\t$np[$c]\t$fl +ag\n"; } else { $flag=$np[$c]; print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$fl +ag\n"; print TRIPLET "$start[$b]\t$stop[$b]\t$np[$b]\t$fl +ag\n"; print TRIPLET "$start[$c]\t$stop[$c]\t$np[$c]\t$fl +ag\n"; } } else { #ambigious data print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$np[$a] +\n"; print TRIPLET "$start[$b]\t$stop[$b]\t$np[$b]\t$np[$b] +\n"; print TRIPLET "$start[$c]\t$stop[$c]\t$np[$c]\t$np[$c] +\n"; } elsif ($blocks[$a]=~m/^\d.+/ && $blocks[$b]=~m/^\d.+/) { if($np[$a] eq $flag || $np[$a] eq $flag) { print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$flag\n +"; print TRIPLET "$start[$b]\t$stop[$b]\t$np[$b]\t$flag\n +"; } else { print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$np[$a] +\n"; print TRIPLET "$start[$b]\t$stop[$b]\t$np[$b]\t$np[$b] +\n"; } } else { print TRIPLET "$start[$a]\t$stop[$a]\t$np[$a]\t$np[$a]\n"; } } else { print TRIPLET "$blocks[$index]\n"; } $index++; $a=+2; $b=+2; $c=+2; } close BLOCKS; close START; close STOP; close NP; close TRIPLET; exit;

the error that i get is syntax error at line 60 near "elsif" syntax error at line 81 near "else" syntax error at line 90 near "}" missing right curly or square brackets at the end

Replies are listed 'Best First'.
Re: unknown syntax error
by Loops (Curate) on Jul 05, 2013 at 05:59 UTC

    You're missing a '}' right above that elsif on line 60. The general style of deeply nested if statements can lead to these problems being hard to spot. You might want to think about how you can refactor the code a bit.

Re: unknown syntax error
by CountZero (Bishop) on Jul 05, 2013 at 06:17 UTC
    Unrelated to this error, but you can write
    for($i=0;$i<(scalar(@blocks));$i++)
    more easy and more "perlish" as
    for my $block (@blocks) {...}
    Then you can replace $blocks[$index] by $block and do away with $i and $index.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: unknown syntax error
by zork42 (Monk) on Jul 05, 2013 at 09:20 UTC
    Unrelated to the error but:

    (1) This is probably wrong as you're ||-ing "$np[$a] eq $np[$b]" with itself:
    34: if($np[$a] eq $np[$b] || $np[$a] eq $np[$b] || $np[$b] eq $np[$c]) ^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^ once and again

    Maybe you meant:
    v v 34: if($np[$a] eq $np[$b] || $np[$a] eq $np[$c] || $np[$b] eq $np[$c]) ^ ^


    (2) Also your rexexps m/^\d.+/ can be simplified very slightly to m/^\d./ ie remove the +.
    (I only mention this incase your rexexps are wrong.)
      hmmm well searched mike :). it seems this is the problem.
Re: unknown syntax error
by Anonymous Monk on Jul 05, 2013 at 05:58 UTC

    look on line 80

Re: unknown syntax error
by sahil2588 (Novice) on Jul 05, 2013 at 07:52 UTC
    There is a missing curly brace at line 60 to end if block.