Can someone please help me understand the following expression?
printf("%3d - %s\n", counter, name)
That line prints something like this 6 - Install Adobe software
I have looked up information and read the reference but I can't find a simple answer and I'm a bit confused. If you can refer me to a good reference, please do so.
%3d Ok, according to what I could understand, %3d is the number of characters or spaces. Please point me to a reference that explains it.
%s\n I couldn't figure out what this does. I guess \n is a newline or something similar, but by looking at the output it doesn't seem to work like that.
Why are counter and name variables separated by commas?
By looking at the output is seems that %3d is kind of replaced by counter and %s\n is replaced by name. I'm not sure how it works but I would like to understand it.
For syntax look at any printf docs, but check the sprintf docs on ruby-doc.
They're separated by commas because they're separate parameters to the function, but that's more or less syntactic sugar. Think varargs.
Not sure what you mean with the %s\n thing, it's a string then a newline: that's what it outputs.
If your question is specifically "how does the code turn a formatting string and a group of arguments into output" I'd probably search for source, for example, a tiny embedded printf. Nutshell version is that the format string is searched for formatting options, they consume their associated parameters, outputting an appropriately-formatted string. It's a tiny little DSL.
Related
I'm trying to pass in /\!|\.|\?/ to the separator argument for readlines. It seems it's not possible. Or is it?
f.readlines(/\!|\.|\?/)
I know the alternative is to use read and split, which accepts Regexp, but I want to know if this is also possible with readlines
IO#readlines expects a string, not a regular expression. But the desired behaviour might be easily achieved with read + split since according to the documentation readlines “reads the entire file”:
f.read.split /\!|\.|\?/
Please also read the valuable comment by #tom-lord with a significant improvement suggestion.
I want to parse the following word in shell script
VERSION=METER1.2.1
Here i want to split it as two words as
WORD1=METER
WORD2=1.2.1
Let me help how to parse it?
Far more efficient than using external tools such is sed is bash's built-in parameter expansion support. For instance, if you want the name variable to contain everything until the first number, and the numbers variable to contain everything after the last alpha character:
version=METER1.2.1
name=${version%%[0-9]*}
numbers=${version##*[[:alpha:]]}
To understand this, see the BashFAQ entry on string manipulation in general, or the BashFAQ entry on parameter expansion in particular.
This is really two questions combined.
First is: Is there any way to do math inside of a here document?
Second: Is there any way to use format strings in a here document?
An example of this second question is this:
print <<HERE
%s
HERE
% 'string'
yet that doesn't work.
Thankss
Yes to both. By default, heredoc does interpolation with #{}. You can put any Ruby code inside it, and have it evaluated. (To avoid interpolation, you can do <<'HERE'.) For your second part, you have the syntax wrong. You should do:
print <<HERE % 'string'
%s
HERE
In answer to your first question. Yes you can do math in a HERE doc. You would just use the standard #{} expression evaluation.
<<EOF
This is a
multiline doc
with some math in it.
#{3 *18}
EOF
In answer to your second question; you can not do string interpolation in the way you are showing in your example within a HERE doc. Consider the way it is evaluated. It is treated more like a stream that is instantly evaluated when the document is ended.
Typically I would just create your other variables prior to the HERE doc and then use the standard expression evaluation within your HERE doc.
If you want to format your strings directly in the HERE doc it needs to go at the beginning as #sawa pointed out. Notice in the following example how I'm passing multiple strings in an array fashion.
<<EOF % ['string','string2','string3']
%s
%s
%s
HERE
I tried to understand the specifications here but they're actually quite difficult to understand.
http://www.yaml.org/spec/1.2/spec.html#id2779048
As far as I can see, there are three ways of wrapping text but their function is very similar... in fact so similar that I don't get the point in having all of them instead of one or two.
Well my problem is that I have some String that is really long (~700 characters) but has no whitespaces.
Now of course I want to put it into multiple lines but there seems to be no way to do so without having any linefeeds or space characters that I do not want.
So is this actually possible?
---
aTest:
hereComes
SomeText
ThatShould
NotHave
AnyWhitespaces
It's possible. See. Is there a way to represent a long string that doesnt have any whitespace on multiple lines in a YAML document?
Quoted example:
"abcdefghi\
jklmnopqr\
stuvwxyz"
Single quotes may also work depending on the parsing library so YMMV.
I am trying to grasp the concept of Regular Expressions but seem to be missing something.
I want to ensure that someone enters a string that ends with .wav in a field. Should be a pretty simple Regular Expression.
I've tried this...
[RegularExpression(#"$.wav")]
but seem to be incorrect. Any help is appreciated. Thanks!
$ is the anchor for the end of the string, so $.wav doesn't make any sense. You can't have any characters after the end of the string. Also, . has a special meaning for regex (it just means 'any character') so you need to escape it.
Try writing
\.wav$
If that doesn't work, try
.*\.wav$
(It depends on if the RegularExpression attribute wants to match the whole string, or just a part of it. .* means 'any character, 0 or more times')
Another thing you should consider is what to do with extra whitespace in the field. Users have a terrible habit of adding extra white space in inputs - its why various .Trim() functions are so important. Here, RegularExpressionAttribute might be evaluated before you can trim the input, so you might want to write this:
.*\.wav[\s]*$
The [\s]* section means 'any whitespace character (tabs, space, linebreak, etc) 0 or more times'.
You should read a tutorial on regex. It's not so hard to understand for simple problems like this. When I was learning I found this site pretty handy: http://www.regular-expressions.info/