Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Regex in a printfile?

by Andrew_Levenson (Hermit)
on Nov 15, 2006 at 19:59 UTC ( [id://584272]=perlquestion: print w/replies, xml ) Need Help??

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

Is it at all possible to modify this:

print OUTFILE (grep { !/^\s*#/ } <INFILE>);

So that it also removes specified characters from the lines that it does print? Yes, this is the same program as last time. I'm hoping I can get it to remove # and anything after that in each line.

Thanks in advance!

Replies are listed 'Best First'.
Re: Regex in a printfile?
by Fletch (Bishop) on Nov 15, 2006 at 20:13 UTC

    That's pretty inefficient as it will read in all of the matching lines from your input before it prints things out.

    That aside, if you want to remove comments from all lines before printing and remove empty lines completely you probably want something more like this.

    while( <INFILE> ) { chomp; s/^\s*#.*$//; next unless length $_; print OUTFILE $_, "\n"; }

    Update: As is remarked below, the s/// needs to be anchored. And yes, this skips empty lines. Now where's my caffeine . . .

      That removes all empty lines, not just those that were comments. Fix:

      while (<INFILE>) { chomp; next if /^\s*#/; s/\s*#.*$//; print OUTFILE "$_\n"; }
        The problem with s/\s*#.*$//; is that you might start breaking scripts unexpectedly. Given this (contrived but not unlikely) code snippet

        my @fred = (1, 2, 3, 4, 5); my $lastSub = $#fred; print qq{$lastSub\n};

        running your substitution will result in

        my @fred = (1, 2, 3, 4, 5); my $lastSub = $ print qq{$lastSub\n};

        Perhaps something like

        s{(?<!\$)#.*$}{};

        would be better but there may be other syntax where the hash is not the start of a comment that I haven't thought of.

        Cheers,

        JohnGG

        Update: Found another non-comment occurrence of the # character in the colour values in Tk programs, e.g.

        -background => #101010,

        so perhaps now

        s{(?<!\$)#(?![0-9a-fA-F]{6}).*$}{};
Re: Regex in a printfile?
by ikegami (Patriarch) on Nov 15, 2006 at 20:24 UTC
    use List::MoreUtils qw( apply ); print OUTFILE apply { s/\s*#.*//; } grep { !/^\s*#/ } <INFILE>;

    If you want to keep the lines that contained nothing but comments, remove the grep.

    List::MoreUtils is not a core module, but it is a very useful module. It can be installed using ppm install List-MoreUtils if you're using ActivePerl.

      Is there any way to do it without using any modules? (Just curious.)

      Thanks!

        huh, there's already a solution that doesn't use any modules in this thread.

        But apply can be implemented using map pretty easily.

        print OUTFILE map { my $s = $_; $s =~ s/\s*#.*/; $s } grep { !/^\s*#/ } <INFILE>;

        You could even merge the map with the grep, but then it looks like spagetti.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://584272]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-23 15:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found