Is it possible to wrap this long line of values into 1 line? I must hard code those values into code and it is very annoying to scroll this block over and over.
There are no newlines in this block.
I commonly use an escape character to break up lines like this.
int long_set[36] = [1,2,3,4 \
5,6,7,...]
The '\' character is used to break up the line.
Related
Line continuation in bash with \ doesn't work if there is a space after the \. This has often resulted in hard to spot bugs in my code.
Also, it prevents me from putting a comment at the end of the line.
Is there a safer way to do line-continuations?
Update: The suggestions in the comments are about making the editor detect the trailing whitespaces. This is a valid approach. But is there a way that doesn't depend on the trailing whitespaces to be so significant?
From the make documentation
Since trailing space characters are not stripped from variable values,
just a space at the end of the line would have the same effect (but be
rather hard to read). If you put whitespace at the end of a variable
value, it is a good idea to put a comment like that at the end of the
line to make your intent clear. Conversely, if you do not want any
whitespace characters at the end of your variable value, you must
remember not to put a random comment on the end of the line after some
whitespace, such as this:
dir := /foo/bar # directory to put the frobs in\
Here the value of the variable dir is ‘/foo/bar ’ (with four
trailing spaces), which was probably not the intention. (Imagine
something like ‘$(dir)/file’ with this definition!)
Is there a way to allow make to parse dir properly so that it is just /foo/bar? Or has the entire make community just accepted that you are not allowed to have comments on the same line with spaces separating them?
seems kind of silly considering end-of-line comments are very useful
I use a vbs variable that contains a large string. This string holds on many lines and some of these lines begin with one or more spaces. I need to delete all spaces at beginning of each line in my variable. To do that, I use a regular expression such as ^\s+, but it only ignores spaces at begin of string and not in all lines as part of my variable.
Please could you please, help me with something that do it?
Thank you in advance
The variable holds a string that contains line break characters, is that? if it is, try setting the property Multiline of the RegExp to true.
From: http://www.xaprb.com/blog/2005/11/04/vbscript-regular-expression-gotchas/
(You could also split each line into an array and do the Regex on a loop on each)
In Ruby, sometimes I need to remove the new line character at the beginning of a string. Currently what I did is like the following. I want to know the best way to do this. Thanks.
s = "\naaaa\nbbbb"
s.sub!(/^\n?/, "")
lstrip seems to be what you want (assuming trailing white space should be kept):
>> s = "\naaaa\nbbbb" #=> "\naaaa\nbbbb"
>> s.lstrip #=> "aaaa\nbbbb"
From the docs:
Returns a copy of str with leading whitespace removed. See also
String#rstrip and String#strip.
http://ruby-doc.org/core-1.9.3/String.html#method-i-lstrip
strip will remove all trailing whitespace
s = "\naaaa\nbbbb"
s.strip!
Little hack to chomp leading whitespace:
str = "\nmy string"
chomped_str = str.reverse.chomp.reverse
To be perfectly accurate chomp not only can delete whitespace, from the end of a string, but can also delete arbitrary characters.
If the latter functionality is sought, one can use:
'\naaaa\nbbbb'.delete_prefix( "\n" )
As opposed to strip this works for arbitrary characters exactly like chomp.
So, just for a bit of clarification, there are three ways that you can go about this: sub, reverse.chomp.reverse and lstrip.
I'd recommend against sub because it's a bit less readable, but also because of how it works: by creating a new string that inherits from your old string. Plus you need a regular expression for something that's fairly simple.
So then you're down to reverse.chomp.reverse and lstrip. Most likely, you want lstrip because it's a bit faster, but keep in mind that the strip operations are not the same as the chomp operations. strip will remove all leading newlines and whitespace:
"\n aaa\nbbb".reverse.chomp.reverse # => " aaa\nbbb"
"\n aaa\nbbb".lstrip # => "aaa\nbbb"
If you want to make sure you only remove one character and that it's definitely a newline, use the reverse.chomp.reverse solution. If you consider all leading newlines and whitespace garbage, go with lstrip.
The one case I can think of for using regular expressions would be if you have an unknown number of \rs and \ns at the beginning and want to trim them all but avoid touching any whitespace. You could use a loop and the more String methods for trimming but it would just be uglier. The performance implications don't really matter that much.
s.sub(/^[\n\r]*/, '')
This removes leading newlines (carriage returns and line feeds, as in chomp), not any whitespace.
Not sure if it's the best way but you could try:
s.reverse.chomp.reverse
if you want to leave the trailing newline (if it exists).
This should work for you: s.strip.
A way to do this for whitespace or non-whitespace characters is like this:
s = "\naaaa\nbbbb"
s.slice!("\n") # returns "\n" but s also has the first newline removed.
puts s # shows s has the first newline removed
I know that you can use this to remove blank lines
sed /^$/d
and this to remove comments starting with #
sed /^#/d
but how to you do delete all the comments starting with // ?
You just need to "escape" the slashes with the backslash.
/\/\//
the ^ operator binds it to the front of the line, so your example will only affect comments starting in the first column. You could try adding spaces and tabs in there, too, and then use the alternation operator | to choose between two comment identifiers.
/^[ \t]*(\/\/|$)/
Edit:
If you simply want to remove comments from the file, then you can do something like:
/(\/\/|$).*/
I don't know what the 'd' operator at the end does, but the above expression should match for you modulo having to escape the parentheses or the alternation operator (the '|' character)
Edit 2:
I just realized that using a Mac you may be "shelling" that command and using the system sed. In that case, you could try putting quotation marks around the search pattern so that the shell doesn't do anything crazy to all of your magic characters. :) In this case, 'd' means "delete the pattern space," so just stick a 'd' after the last example I gave and you should be set.
Edit 3:
Oh I just realized, you'll want to beware that if you don't catch things inside of quotes (i.e. you don't want to delete from # to end of line if it's in a string!). The regexp becomes quite a bit more complicated in that case, unfortunately, unless you just forgo checking lines with strings for comments. ...but then you'd need to use the substitution operation to sed rather than search-and-delete-match. ...and you'd need to put in more escapes, and it becomes madness. I suggest searching for an online sed helper (there are good regex testers out there, maybe there's one for sed?).
Sorry to sort of abandon the project at this point. This "problem" is one that sed can do but it becomes substantially more complex at every stage, as opposed to just whipping up a bit of Python to do it.