Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: how do I print content of a cpp file without cpp comments?

by hgolden (Pilgrim)
on Sep 19, 2006 at 13:37 UTC ( [id://573713]=note: print w/replies, xml ) Need Help??


in reply to how do I print content of a cpp file without cpp comments?

Update: Pardon my well-intentioned mistake. See comments below.

Hey

I tried to stick close to your structure, and I did this a little unconventionally to avoid the multi-line match. I'm sure that someone else will tell you (correctly) that this is more cleanly done with such a match, but I think it's easier to mimic your structure this way, and that helps to see what's going on.

use warnings; open(CITY, "city.cpp.txt") || die("sorry, file couldn't be opened. $!" +); while ($content=<CITY>) { if ($content!~m#//|/\*#) { print "$content"; } elsif ($content=~m#/\*#) { while (<CITY>!~m#\*/#) { } } }
This program works by printing lines that don't have comment bars and aren't the beginning of multiline comments. If a line is the beginning of a multiline comment, it does a while loop that doesn't do anything except skip through lines until it finds the end of the comment. Now when the filehandle is passed back to your initial while loop, the next line is the one after the multiline comment.

Let me know if you need more help figuring out how this works.

Hays

Replies are listed 'Best First'.
Re^2: how do I print content of a cpp file without cpp comments?
by jarich (Curate) on Sep 19, 2006 at 13:45 UTC

    I came to C++ from a C background, and thus I often wrote code as follows:

    /* This does something cool */ do_something_cool(); /* This doesn't */ //....

    Which is to point out that your current example has a subtle bug wherein it appears you'll drop all of the text in my example, rather than just the 3 lines of comments.

    Still, I'm sure jsmith can work on that.

    All the best,

    jarich
Re^2: how do I print content of a cpp file without cpp comments?
by davorg (Chancellor) on Sep 19, 2006 at 13:57 UTC

    It's been a while since I've written much C or C++, but doesn't your code have a slight problem when the comment character sequences appear in quoted strings?

    Which (along with jarich's bug) just goes to illustrate the problems of trying to parse complex data using regular expressions. You'll almost certainly fall over subtle edge cases unless you use a real parser.

    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      Yep, I concede. File::Comments it is. This is what happens when you learn Perl without going through C first, and forget to always check CPAN :)

      Hays

Re^2: how do I print content of a cpp file without cpp comments?
by jsmith (Initiate) on Sep 19, 2006 at 13:39 UTC
    thanks a bunch!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-18 22:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found