in reply to Re^2: removing comments
in thread removing comments

Your question is a bit ambigious. Do you want to
  1. create a new file with no comments
  2. load the file into memory and strip the comments before further processing
?

variant 1:
open (my $in, "<", $ARGV[0]) or die "in: $@"; open (my $out, ">", "$ARGV[0].new") or die "out: $@"; while( my $line = <$in>) { print $out $line unless $line =~ /^#/; } close $in; close $out;
variant 2:
open (my $in, "<", $ARGV[0]) or die "in: $@"; my @lines = grep { not /^#/ } <$in>; #<> here in list context, therefo +re no explicit loop
Note that you cannot erase a line from the middle of a file, you will always have to write a new one. But you already knew that, didn't you? ;-)


holli, /regexed monk/

Replies are listed 'Best First'.
Re^4: removing comments
by GrandFather (Saint) on Jun 21, 2007 at 20:42 UTC

    Or, given the file name is in @ARGV anyway:

    $^I = '.bak'; /^#/ or print while <>;

    to in place edit and generate a backup file.


    DWIM is Perl's answer to Gödel