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

hiii
i am getting sm compilation errors while executing the below code can any one help me out with this
use strict; use warning; print "\n Enter The file name :>"; chomp(my $filename1=<STDIN>); open(FILE,"<",$filename1) or die "\n...Cannot find the file: $filename +"; open (W, "> Result.txt"); my @line; while (<FILE>) { push (@line, $_); my $count++; } print W $line [0]; for ($i = 0; $i < $count; $i++) { @st = split (' ', $line [$i]); if (($st [4] eq /chr1/) && ($st [10] eq /chr1/)) { print W $line [$i]; } }

Replies are listed 'Best First'.
Re: pattern matching compilation errors
by choroba (Cardinal) on Jun 26, 2012 at 22:07 UTC
    I am getting the following error. Are you getting the same one?
    Can't locate warning.pm in @INC (...
    OK, so change warning to warnings only to get the rest of the errors:
    Global symbol "$filename" requires explicit package name at 2.pl line +7. Global symbol "$i" requires explicit package name at 2.pl line 16. Global symbol "$i" requires explicit package name at 2.pl line 16. Global symbol "$count" requires explicit package name at 2.pl line 16. Global symbol "$i" requires explicit package name at 2.pl line 16. Global symbol "@st" requires explicit package name at 2.pl line 18. Global symbol "$i" requires explicit package name at 2.pl line 18. Global symbol "@st" requires explicit package name at 2.pl line 19. Global symbol "@st" requires explicit package name at 2.pl line 19. Global symbol "$i" requires explicit package name at 2.pl line 21.
    Seems like scoping problems. Here is the code fixed with explanation:
    use strict; use warnings; # Already mentione +d. print "\n Enter The file name :>"; chomp(my $filename1 = <STDIN>); open(FILE,"<",$filename1) or die "\n...Cannot find the file: $filename1"; # Missing 1 at the + end of the message. open (W, "> Result.txt"); my @line; my $count; # To be declared o +utside the loop to survive. while (<FILE>) { push (@line, $_); $count++; # Using 'my' here +means $count is initialized } # to 0 in each ite +ration of the loop. print W $line [0]; for (my $i = 0; $i < $count; $i++) # $i was not decla +red { my @st = split (' ', $line [$i]); # @st was not decl +ared if (($st [4] eq /chr1/) && ($st [10] eq /chr1/)) { print W $line [$i]; } }
    Now the code has no compilation errors. It probably still does not work, though. Happy debugging!
Re: pattern matching compilation errors
by AnomalousMonk (Archbishop) on Jun 26, 2012 at 22:24 UTC
    i am getting sm compilation errors...

    It's always a very good idea to say just what errors you are getting, but I can make a few guesses.

    while (<FILE>) { push (@line, $_); my $count++; }

    The
        my $count++;
    statement in the  while loop above creates and then increments a lexical variable each time through the loop. The value of this variable will never be other than 1 because it goes out of existence at the end of the loop. That's why Perl complains (I'm guessing) about accessing the  $count variable in the subsequent  forloop: the variable has never been defined in any current scope.

    In the  for loop, the  if statement
        if (($st [4] eq /chr1/) && ($st [10] eq /chr1/)) { ... }
    is wrong because the // match operator binds to (ie., matches against) a variable via the  =~ binding operator, not the  eq operator. Without explicit binding, it binds to the  $_ default scalar.

    Not errors, but...

    The whole  while loop can be replaced by
        my @line = <FILE>;
    and then the number of elements in the array (i.e., the number of lines) will be
        my $count = @line;
    but this variable is not really needed since the  for loop can be better expressed as
        for my $l (@line) { do_something_with_line($l); }

    Update: As Kenosis has pointed out, it is better practice to use the three-argument form of open:
        open my $fh, '<', $filename1 or die "opening '$filename1': $!";

Re: pattern matching compilation errors
by Kenosis (Priest) on Jun 26, 2012 at 22:26 UTC

    choroba did a great job fielding some of your coding issues. With some repeats, here's the following:

    use strict; use warnings; #<<-- warnings print "\n Enter The file name :>"; chomp( my $filename1 = <STDIN> ); open( FILE, "<", $filename1 ) or die "\n...Cannot find the file: $filename1"; #<<-- $filename1" open( W, "> Result.txt" ); my @line; while (<FILE>) { push( @line, $_ ); my $count++; } print W $line[0]; for ( my $i = 0 ;$i < $count ;$i++ ) #<<-- my $i; $count, from above, +has dropped outside of scope and is now undefined. Update: use $i < $ +#line+1 { my @st = split( ' ', $line[$i] ); #<<-- my @st if ( ( $st[4] eq /chr1/ ) && ( $st[10] eq /chr1/ ) ) { print W $line[$i]; } }

    Consider the following reworking (untested):

    use Modern::Perl; print "\n Enter the file name :>"; chomp( my $filename1 = <STDIN> ); open my $fh, '<', $filename1 or die "Cannot find the file $filename1: +$!"; my @line = <$fh>; close $fh; open my $w, '>', 'Result.txt' or die "Cannot open the file Result.txt: + $!"; print $w $line[0]; for (@line) { my ( $fourth, $tenth ) = ( split ' ' )[ 4, 10 ]; print $w $_ if $fourth eq 'chr1' and $tenth eq 'chr1'; # did you mean $fourth =~ /chr1/ and $tenth =~ /chr1/ } close $w;

    Hope this helps!

Re: pattern matching compilation errors
by muba (Priest) on Jun 27, 2012 at 01:39 UTC

    Without wanting to repeat what others said before me, there's one part in this line that stood out to me.

    open(FILE,"<",$filename1) or die "\n...Cannot find the file: $filename +";

    Namely, the error message. "Cannot find the file" might not be the best to put there. There could be all sorts of other reasons the file couldn't be opened. Perhaps the process doesn't have the right permissions, maybe the file is locked, maybe there are tons of other things that could go wrong. But that's okay, because you can make Perl tell you (more or less) exactly what went wrong!

    ... or die "\n... Can't open $filename: $!\n";

      Good eye, muba!