in reply to Re^3: skipping lines when parsing a file
in thread skipping lines when parsing a file

ssandv,
Ok, this is pretty simple and straight forward. The regex captures the
capital letter(word) at the beginning of the line and checks for the next
line that begins with a capital word. When it comes across that line
it starts printing again?
When I run it
#!/usr/bin/perl -w use strict; =cut The script parses the targeting.gb file and creates a new file that contains removes the comment info. =cut open(my $in, "C:/Documents and Settings/mydir/Desktop/TARGETING.gb"); open(my $out, ">C:/Documents and Settings/mydir/Desktop/TARGET.gb"); my $state; while(my $line =<$in>){ if ($line=~/^([A-Z]+)/) { $state=$1; } print $out $line unless $state eq "COMMENT"; } close $in; close $out

The file is not being parsed. The out file is still the same as the in.
Any idea?
LomSpace

Replies are listed 'Best First'.
Re^5: skipping lines when parsing a file
by ssandv (Hermit) on Aug 20, 2009 at 17:46 UTC

    A couple thoughts come to mind. First of all, you're better off to be in the habit of using 3-argument open, because it protects you against weird filenames. This shouldn't be a problem in that case, but it's just a good habit. Also, use warnings; as well as using strict. Most importantly, your open commands should also have or die "open failed: $!" (or something similar) at the end, so that if they fail you know about it. In this case, if you try to open a locked or read-only file for writing, it will die (because open returns 0 if it can't open the file) instead of continuing on and silently failing on all its print statments, which is what's probably happening to you.

    #!/usr/bin/perl -w use strict; use warnings; my $readfile = "C:/Documents and Settings/mydir/Desktop/TARGETING.gb"; my $writefile = "C:/Documents and Settings/mydir/Desktop/TARGET.gb"; open my $in, "<", $readfile or die "couldn't open $readfile: $!"; open my $out, ">", $writefile or die "couldn't open $writefile: $!"; my $state; while(my $line =<$in>){ if ($line=~/^([A-Z]+)/) { $state=$1; } print $out $line unless $state eq "COMMENT"; } close $in; close $out

    The output I produced above in Re:^3 was done using the code I wrote, so that's what drives me to look at this as a file access issue, now.