Stripping Comments from a LSL Script

How to easily remove the comment lines from a LSL script

This article explains how to easily strip single comment lines from scripts. Typically these comment lines start with either // or # characters. Second Life’s® and OpenSims lsl scripting language, PHP, and other c++ style scripts use //. Shell style scripts use comments that start with #. Either style of comment lines may be easily removed from your script in a single operation using a free, online text editing tool.


I put a lot of comments in my scripts, because I forget stuff easily. Some of my comments would seem amateurish to a more experienced coder, and I’m a bit embarrassed to publish a script with them in it! I suppose some people may also have comments that they consider “proprietary” and don’t want others to see should they hack into the script. There are a lot of reasons you might want to remove some or all of the comments from your script.  Removing the comments and all the unneeded blank space in a script condenses the code and makes it difficult to read and edit (for you as well as anyone else, so keep a copy of the original!)


One reason NOT to remove comments from Second Life and OpenSim lsl scripts is to make the script more efficient. When you save a script in the viewer the viewer’s script compiler automatically removes all of the comments, white space, and blank lines from the executable version that Second Life uses in-world. So stripping the comments out yourself will not make the script more efficient. I believe this is true of most other compiled script languages also.


Tip: If I have some comments I want to leave, and others I want to remove, I put a # symbol as the first character in each comment line I want to remove. That way I can just strip out some comments, while leaving others. For example:

//This is a standard comment that I want to remain.
//# This is a comment I will want to remove later.



Step-by-Step Instructions


These instructions use the SED Stream Editor to strip the comments from a script. A free online version of the SED Stream Editor is located at the website http://ideone.com.  If that website should ever disappear try searching for "SED editor" and you can find another one online or a standalone version to download.

Here's what you'l see when you open the SED editor:




The Steps above:

1. Select “Bash” as language on left.

2. Copy & paste the Bash SED code into the “enter your source code” text box. See the SED Code Samples below. This is the instructions the editor uses to determine what you wish for it to do.

3. Click on the link labeled “click here to enter input (stdin) or additional notes”

4. Copy and paste your script into the “enter your input (stdin)” text box.

5. Click on “Submit” button in the left column. Wait for the editor to finish processing your script.

6. A result page (showing your input and output script) will display after the script is processed (see image below.)  On the result page a light blue box is labeled input, this has a copy of the code you input. Ignore it and scroll down to the second box labeled “output”. The output box contains your script with the comments stripped out. That’s it! Copy and paste it to your script file.




SED Code Samples


I suggest trying the one directly below first, it makes a file with cleaner results. Here’s what it does: It starts by removing comments starting with //, then it removes any whitespace, and finally it removes all the blank lines from your script:
sed -e 's///.*//' -e 's/ *$//' -e '/^$/d'

This does the same thing as the one above, but only removes comments starting with //#:
sed -e 's///#.*//' -e 's/ *$//' -e '/^$/d'

Here is the same thing again, this time it only removes lines starting with #. This is useful for shell style script languages that use the # symbol for comment lines.
sed -e 's/#.*//' -e 's/ *$//' -e '/^$/d'


More SED code snipets you may find useful


This removes standard lsl comments starting with // :
sed 's///.*//'

This removes //# and anything after it on the same line:
sed 's///#.*//'

This removes # and anything after it on the same line:
sed 's/#.*//'

This removes multiple blank spaces:
sed 's/ *$//'

This removes blank lines:
sed '/^$/d'


No comments:

Post a Comment