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

Good morning! I've this code:

use strict; use warnings; use Data::Dumper; open (FILE, "/Users/Pabli/Desktop/gene_association.goa_human"); open (FILE2, "/Users/Pabli/Desktop/go3.obo"); $main::mio; $main::mio2; $main::mio4; $main::words2; my $i=0; my %go_accession_hash=(); while(<FILE>) { my @array_with_all_fields=split(/\t/); if ($array_with_all_fields[2] eq "TP53"){ $mio=$array_with_all_fields[4]; open my $out_file, '>>', 'myoutputfilename.txt' or die "$!"; #print "".$mio."\n"; print $out_file $mio; # print to file } } open (FILE3, "/Users/Pabli/Documents/workspace/PerlProva/myoutputfilen +ame.txt"); while($line=<FILE2>){ @words2 = split(" ",$line); #print "$words2[1]\n"; $i=5; while($line2=<FILE3>){ $i = $i - 1; if ($i >= 0) { @words = split(" ",$line2); } } print $words[0]."\n"; #if ($words2[0] eq "id:") { #$mio4=$words2[1]; #print $mio4."\n"; # } #elsif ($words2[0] eq "name:"){ #if ($mio4 == $mio2){ #print "name: ".$words2[1]."\n"; #} #} #} } close FILE; close FILE2; close FILE3;

The program out of second "while" prints "$words[0]" not for one time (single row) but for a lot of time! I don't understand the reason! There is a solution? Can someone help me? Thanks Paola

Replies are listed 'Best First'.
Re: While problem
by marto (Cardinal) on Oct 27, 2014 at 09:38 UTC

    "Can someone help me?"

    You could fix the problems perl tells you about when you try to run the script?

    Variable "$mio" is not imported at x.pl line 18. Variable "$mio" is not imported at x.pl line 22. Variable "@words2" is not imported at x.pl line 31. Global symbol "$mio" requires explicit package name at x.pl line 18. Global symbol "$mio" requires explicit package name at x.pl line 22. Global symbol "$line" requires explicit package name at x.pl line 30. Global symbol "@words2" requires explicit package name at x.pl line 31 +. Global symbol "$line" requires explicit package name at x.pl line 31. Global symbol "$line2" requires explicit package name at x.pl line 34. Global symbol "@words" requires explicit package name at x.pl line 38. Global symbol "$line2" requires explicit package name at x.pl line 38. Global symbol "@words" requires explicit package name at x.pl line 43. Execution of x.pl aborted due to compilation errors.

    You've been told this before. Also, use the three argument open, and report on errors (for exmaple ... or die "cannot open < input.txt: $!";).

      There isn't any errors!!! This is the outpur of the compiler:

      GO:0000060GO:0000122GO:0000979GO:0001077GO:0001701GO:0001756 </p> <p> GO:0000060GO:0000122GO:0000979GO:0001077GO:0001701GO:0001756 </p>
      I want for example only the first row!! Thanks!!

        "There isn't any errors!!!"

        Then you are not running the code you posted here. Don't bother posting code here which you aren't actually running, it's a waste of our time. As roboticus pointed out previously:

        "Try posting the code you actually run or don't gratuitously paste in a "use strict; use warnings;" at the beginning if you're not actually using them."

        Then you are not running the script that you posted. Please make sure that the code you post and the code you run are the same.

        For what it's worth, I get all the same errors marto saw. If you're not seeing any errors, what motivated these lines at the top of your script? :

        $main::mio; $main::mio2; $main::mio4; $main::words2;

        In your script there are a number of variables that haven't been prefixed with "my" when initially declared. Fixing those would be a good first step.