Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8974485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:41:31+00:00 2026-06-15T18:41:31+00:00

I am trying to comment out all the preprocessing instructions of the type: if

  • 0

I am trying to comment out all the preprocessing instructions of the type:

if LABEL
  do something
else
  do something else
end 

You have to provide a list of LABELS to the script so that it only comments out the line containing that “if” statements of interest and all the code under the “else” part of the statement (if any – There may or may not be an “else”).
Note that there could be other type of preprocessing instructions (all ending with the keyword “end”), which increases the difficulty when there are multiple level of nesting. So, we have to make sure to remove the correct else/end associated to each “if LABEL_OF_INTEREST”.
In order to comment out a line, use #.

In order to make things clearer, let’s take an example, where only the code from within the “if” statements have to be preserved, the rest need to be commented out. Here is the input:

if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful  
   while A < 1
      if STATEMENT_TO_IGNORE  
          do somthing useful  
          do somthing else useful  
      else  
          do nothing  
          do nothing  
      end  
   end
else  
   do nothing  
   do nothing        
end  
if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful
end     

The expected result would be:

 #if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful  
   while A < 1
      if STATEMENT_TO_IGNORE  
          do somthing useful  
          do somthing else useful  
      else  
          do nothing  
          do nothing  
      end  
   end
  #else  
  #   do nothing  
  #   do nothing        
  #end 
#if STATEMENT_TO_KEEP  
   do somthing useful  
   do somthing else useful
#end 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T18:41:31+00:00Added an answer on June 15, 2026 at 6:41 pm
    use strict;
    use warnings;
    
    my $state = 0;                       # print out when this is 0
    while (<>) {
      if (!s/^(\s*if)/#$1/) {            # when you find and if, comment out
        if (!$state and /^\s*else/) {    # turn off printing from else blocks
          $state = 1;
          s/^/#/;                        # and comment out 'else'
        }   
        if ($state) {
          s/^/#/;                        # check flag, and comment out if set
          if (/^\s*end\s*$/) {
            $state = 0;                  # clear flag upon an end
          }   
        }   
      }
      print;                             # actual print
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to figure out why this works: >>> comments = Comment.objects.all() >>>[c.content_object for
I'm trying to figure out how I can have a comment header automatically added
Scraping pages using BeautifulSoup; trying to filter out links that end in ...html#comments Code
I'm stumped. I'm trying to get a list of all the email address a
I am trying to overload operator<< in my code. If I comment out the
I have been trying to work out a file rename program based on ruby,
I am trying to find out how method calls works in objective-c. I have
I've been having a huge headache trying to figure out why the facebook comments
I am trying to get the comment count for a video entry but this
I'm trying to code a page where you can post a comment without reloading

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.