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

Let's say I have the file.

# A B C # D E F #

I'm having difficulties with the logic. I enter and $INFILE (like the one above). Every time '#' is reached it indicates the end of the temp file. This temp file will be sent to two other subroutines for processing and it's output appended to a file.

I can't seem to get the temp file to overwrite.

First temp file should have this in it. # A B C
Second temp file should have this in it. # D E F

Here's what I've written so far. I'm mainly having problems with the scope of the filehandle. I've spent an embarrassing amount of time on this and would like to understand how to do it and move on.

#!/usr/bin/perl5.8.8 use strict; use warnings; print "Enter file to process\t"; my $count = <>; #captures input from STNDIN and finds the file associa +ted with that number chomp $count; tempProteinFamFileCreator(); #creates a temp file with protein family sub tempProteinFamFileCreator { my $infile = $count."_ProFam"; #iterates through all the protein famil +y files with the count variable open (my $INFILE,"<", $infile); my $flag = 0; while(<$INFILE>) { my $TEMPfa; if ($_ =~/^#/ && $flag == 0) { open ($TEMPfa,">", 'temp'); $flag++; } if ($_ =~/^[\w\d]+/) { chomp $_; print $TEMPfa "$_\n"; } if (($_ =~/^#/ && $flag == 1) || $_ =~/^>File/) { close ($TEMPfa); MuscleHMMERsearch(); TableParser(); } } close ($INFILE); #closes the $INFILE handle } #end of subroutine tempProteinFamFileCreator

Forever Thanks.

I've also tried...

#!/usr/bin/perl5.8.8 use strict; use warnings; print "Enter file to process\t"; my $count = <>; #captures input from STNDIN and finds the file associa +ted with that number chomp $count; tempProteinFamFileCreator(); sub tempProteinFamFileCreator { my $infile = $count."_ProFam"; #iterates through all the protein famil +y files with the count variable open (my $INFILE,"<", $infile); my $flag = 0; open (my $TEMPfa,">",'temp'); while(<$INFILE>) { if ($_ =~/^#/ && $flag == 0) { $flag++; } else { if ($_ =~/^[\w\d]+/) { chomp $_; print $TEMPfa "$_\n"; } if (($_ =~/^#/ && $flag == 1) || $_ =~/^>File/) { close ($TEMPfa); MuscleHMMERsearch(); TableParser(); open (my $TEMPfa,">",'temp'); } } } close ($INFILE); #closes the $INFILE handle } #end of subroutine tempProteinFamFileCreator

Replies are listed 'Best First'.
Re: Overwriting temp file
by Anonymous Monk on Sep 21, 2011 at 18:41 UTC

    Your scopes are fine. But this:

    open ($TEMPfa,"<",  'temp');

    should be this:

    open ($TEMPfa,">",  'temp');

    That is, you are opening a file for reading, not writing.

      Thanks! I updated it on the post. It runs, but it doesn't overwrite. I'm going to keep working on it.
Re: Overwriting temp file
by Anonymous Monk on Sep 21, 2011 at 18:52 UTC

    The data you posted does not contain any lines beginning with ##UniRef90

    Your program does not work with the data you posted

    Instead of posting data that resembles the data your program is written to match you need to post actual sample data that your program is actually written to match

    How do I post a question effectively?

      I would but it's large, I'll modify the code in the post.

        I would but it's large, I'll modify the code in the post.

        Well it doesn't have to be real live realsize data, it just has to match your regexes, ie

        #!/usr/bin/perl -- #~ 2011-09-21-11:45:38PDT by Anonymous Monk #~ perltidy -csc -otr -opr -ce -nibc -i=4 use strict; use warnings; use autodie; # dies if open/close... fail Main( @ARGV ); exit( 0 ); sub Main { if ( @_ == 1 ) { NotDemoMeaningfulName( @_, 'temp', \&DiddleSomeTemp ); } else { Demo(); print '#' x 33, "\n", Usage(); } } ## end sub Main sub NotDemoMeaningfulName { my ( $inputFile, $outputFile, $callBack ) = @_; my $flag = 0; my $outMode = '+>'; open my ($inFh), '<', $inputFile; open my ($outFh), $outMode, $outputFile; while (<$inFh>) { if (/^#/) { if ( not $flag ) { $flag++; close $outFh; open $outFh, $outMode, $outputFile; } else { seek $outFh, 0, 0; $callBack->($outFh); close $outFh; open $outFh, $outMode, $outputFile; } ## end else [ if ( not $flag ) ] } else { print $outFh $_; } } ## end while (<$inFh>) close $inFh; close $outFh; } ## end sub NotDemoMeaningfulName sub Usage { <<"__USAGE__"; $0 $0 dataFile perl ${\__FILE__} perl ${\__FILE__} dataFile __USAGE__ } ## end sub Usage sub DiddleSomeTemp { warn "Hey diddle diddle @_\n"; my $f = shift; warn "#$_" while <$f>; } sub Demo { my ( $Input, $WantedOutput ) = DemoData(); my $Output; require Test::More; NotDemoMeaningfulName( $Input, \$Output, sub { Test::More::is( $Output, $WantedOutput->[0], ' NotDemoMeaningfulName Works Aas Designed' ); shift @$WantedOutput; }, ); Test::More::done_testing(); } ## end sub Demo sub DemoData { #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=1 my $One = <<'__One__'; # A B C # D E F # __One__ my @Two = ( #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=2 <<'__Two__', A B C blah __Two__ #~ http://perlmonks.com/?abspart=1;displaytype=displaycode;node_id=927 +193;part=3 <<'__Two__', D E F blah __Two__ ); return \$One, \@Two; } ## end sub DemoData __END__ $ perl pm.927193.pl not ok 1 - NotDemoMeaningfulName Works Aas Designed # Failed test ' NotDemoMeaningfulName Works Aas Designed' # at pm.927193.pl line 76. # got: 'A # B # C # ' # expected: 'A # B # C blah # ' not ok 2 - NotDemoMeaningfulName Works Aas Designed # Failed test ' NotDemoMeaningfulName Works Aas Designed' # at pm.927193.pl line 76. # got: 'D # E # F # ' # expected: 'D # E # F blah # ' 1..2 ################################# pm.927193.pl pm.927193.pl dataFile perl pm.927193.pl perl pm.927193.pl dataFile # Looks like you failed 2 tests of 2.

        As you can see, the logic is sound, temp gets overwritten, pretty much what you already have

        Note the expected data doesn't match on purpose ;)