in reply to Support for hash comments on a line

This will break if you use a # in your "real" data, but I generally would use something like this:
while (<>) { chomp; s/\s*#.*//; # Strip off whitespace and trailing comments next if /^\s*$/; # Skip blank lines &process_line_of_input($_); }

Replies are listed 'Best First'.
Re: Re: Support for hash comments on a line
by c (Hermit) on Nov 01, 2001 at 20:15 UTC
    When i run your code on my file:

    hostname3 # this is a comment for hostname3
    I get:

    Use of uninitialized value in substitution (s///) at ./comment line 11 +, <FH> line 1.

    my full code is:

    #!/usr/bin/perl -w use strict; open(FH, "comments.txt") or die "cant open file"; while (my $line = <FH>) { chomp $line; next if $line =~ /^$/; $line = s/\s*\#.*//; print "|$line|\n"; } close(FH);

    humbly -c

      You want to use =~ instead of = when doing regexp substitions on a variable. You're basically doing this:
      $line = ($_ =~ s/\s*\#.*//);
      Since $_ is undefined here, you get that warning.