basic string concatenation syntax confirmation - basic

I'm new to basic.
Does the following line mean the string ":PULS:WIDT1 " concatenates with a string variable named Te?
":PULS:WIDT1 "&VAL$(Te);

Does the following line mean the string ":PULS:WIDT1 " concatenates with a string variable named Te?
The line is incomplete. Even if this were an accepted syntax and concatenation somehow took place, where would the result go?
The ampersand character (&) is indeed a string concatenation operator in BASIC, but so is the plus character (+). Not every BASIC allows both, but most will allow at least one.
Inserted in a syntactically correct statement, your expression
":PULS:WIDT1 "&VAL$(Te)
would concatenate the ":PULS:WIDT1 " string litteral with the contents of an element of the string array VAL$() indexed by the numerical Te variable.
Although VAL happens to be the name of a built-in function, many BASICs don't mind that you name one or more user variables the same as a keyword.

Related

A string interpolation within concatenation is producing two double quotes instead of one (JuliaLang)

I am trying to include a single double quote in a string during a concatenation within JuliaLang, as below:
tmpStr = string(tmpStr, string("graph [label=\" hi \"]; "))
The output in the text file written with writedlm is:
graph [label="" hi ""]
How can I modify the string interpolation to include only a single double quote instead of this repetition?
The extra double quotes come from writedlm. writedlm uses standard CSV escaping method, which surrounds special characters with double quotes, and uses "" to represent a single double quote. This is OK, as long as you do the inverse transformation when reading the file.
A good method to trace such problems is to create a minimal working example. In this case, something like:
writedlm("tst.tst",["\""])
Which writes tst.tst, but tst.tst now has:
""""
But when read properly:
julia> data = readdlm("tst.tst")
1×1 Array{Any,2}:
"\""
As expected.
Another option to avoid getting the extra quotes is to add quotes=false as an option to writedlm, as in the following example:
julia> writedlm(STDOUT,["\""],quotes=false)
"

Macros and string interpolation (Julia)

Let's say I make this simple string macro
macro e_str(s)
return string("I touched this: ",s)
end
If I apply it to a string with interpolation, I
obtain:
julia> e"foobar $(log(2))"
"I touched this: foobar \$(log(2))"
Whereas I would like to obtain:
julia> e"foobar $(log(2))"
"I touched this: foobar 0.6931471805599453"
What changes do I have to make to my macro declaration?
It's better to parse the string at compile-time than to delegate to Julia. Basically, put the string into an IOBuffer, scan the string for $ signs, and use the parse function whenever they come up.
macro e_str(s)
components = []
buf = IOBuffer(s)
while !eof(buf)
push!(components, rstrip(readuntil(buf, '$'), '$'))
if !eof(buf)
push!(components, parse(buf; greedy=false))
end
end
quote
string($(map(esc, components)...))
end
end
This doesn't work with escaped $ characters, but that can be resolved with some minor changes to handle \ also. I have included a basic example at the bottom of this post.
I wrote it this way because string macros are generally not for emulating Julia strings — regular macros with regular string literals are better for that purpose. So writing up the parsing yourself isn't that bad, especially because it allows customized extensions. If you really want parsing to be identical to how Julia parses it, you could escape the string and then reparse it, as #MattB suggested:
macro e_str(s)
esc(parse("\"$(escape_string(s))\""))
end
The resulting expression is a :string expression which you could dump and inspect, and then analyse the usual way.
String macros do not come with built-in interpolation facilities. However, it is possible to manually implement this functionality. Note that it is not possible to embed without escaping string literals that have the same delimiter as the surrounding string macro; that is, although """ $("x") """ is possible, " $("x") " is not. Instead, this must be escaped as " $(\"x\") ".
There are two approaches to implementing interpolation manually: implement parsing manually, or get Julia to do the parsing. The first approach is more flexible, but the second approach is easier.
Manual parsing
macro interp_str(s)
components = []
buf = IOBuffer(s)
while !eof(buf)
push!(components, rstrip(readuntil(buf, '$'), '$'))
if !eof(buf)
push!(components, parse(buf; greedy=false))
end
end
quote
string($(map(esc, components)...))
end
end
Julia parsing
macro e_str(s)
esc(parse("\"$(escape_string(s))\""))
end
This method escapes the string (but note that escape_string does not escape the $ signs) and passes it back to Julia's parser to parse. Escaping the string is necessary to ensure that " and \ do not affect the string's parsing. The resulting expression is a :string expression, which can be examined and decomposed for macro purposes.

In Swift how to obtain the "invisible" escape characters in a string variable into another variable

In Swift I can create a String variable such as this:
let s = "Hello\nMy name is Jack!"
And if I use s, the output will be:
Hello
My name is Jack!
(because the \n is a linefeed)
But what if I want to programmatically obtain the raw characters in the s variable? As in if I want to actually do something like:
let sRaw = s.raw
I made the .raw up, but something like this. So that the literal value of sRaw would be:
Hello\nMy name is Jack!
and it would literally print the string, complete with literal "\n"
Thank you!
The newline is the "raw character" contained in the string.
How exactly you formed the string (in this case from a string literal with an escape sequence in source code) is not retained (it is only available in the source code, but not preserved in the resulting program). It would look exactly the same if you read it from a file, a database, the concatenation of multiple literals, a multi-line literal, a numeric escape sequence, etc.
If you want to print newline as \n you have to convert it back (by doing text replacement) -- but again, you don't know if the string was really created from such a literal.
You can do this with escaped characters such as \n:
let secondaryString = "really"
let s = "Hello\nMy name is \(secondaryString) Jack!"
let find = Character("\n")
let r = String(s.characters.split(find).joinWithSeparator(["\\","n"]))
print(r) // -> "Hello\nMy name is really Jack!"
However, once the string s is generated the \(secondaryString) has already been interpolated to "really" and there is no trace of it other than the replaced word. I suppose if you already know the interpolated string you could search for it and replace it with "\\(secondaryString)" to get the result you want. Otherwise it's gone.

Matlab: How to delete prefix from strings

Problem: From TrajCompact, i find all the prefix and the value after prefix, using regexp, with this code:
[digits{1:2}] = ndgrid(0:4);
for k=1:25
matches(:,k)=regexp(TrajCompact(:,1),sprintf('%d%d.*',digits{1}(k),digits{2}(k)),'match','once');
end
I want only the postfix of matches, how can delete the prefix from matches?
Method using regular expressions
You can put the .* section in a group by enclosing it in parenthesis (i.e. (.*)). Matlab has some peculiar 'token' nomenclature for this. In any case, an example of how it works:
[match, group] = regexp('25blah',sprintf('%d%d(.*)',2,5),'match','once','tokens');
Then:
match would be a char array containing '25blah'
group would be a 1x1 cell array containing the string 'blah'.
That is, the variable group would hold what you're looking for.
Hack method
Since your prefix is always two digits, you could also just take everything from the 3rd character of the match onwards:
my_string = match(3:end);
other comments
You may want to require the prefix to occur at the beginning of the string by adding ^ to the beginning of your regular expression. Eg., make the line:
[match, group] = regexp('25blah',sprintf('^%d%d(.*)',2,5),'match','once','tokens');
As it is, your current regular expression would match strings like zzzzzzzzz25stuff. I'm not sure if you want that (assuming it can occur in your data).

Meaning of $ in a string?

I came along this
__date__ = "$Date: 2011/06$"
and found this in the docs
$$ is an escape; it is replaced with a single $.
$identifier names a substitution placeholder matching a mapping key of "identifier". By default, "identifier" must spell a Python identifier. The first non-identifier character after the $ character terminates this placeholder specification.
${identifier} is equivalent to $identifier. It is required when valid identifier characters follow the placeholder but are not part of the placeholder, such as "${noun}ification".
but I don't understand it.
Could someone explain in plain english what's the $ for and give some examples preferably?
To Python, those dollar signs mean nothing at all. Just like the 'D' or 'a' that follow, the dollar sign is merely a character in a string.
To your source-code control system, the dollar signs indicate a substitution command. When you check out a new copy of your source code, that string is replaced with the timestamp of the last committed change to the file.
Reference:
http://svnbook.red-bean.com/en/1.6/svn.advanced.props.special.keywords.html
http://www.badgertronics.com/writings/cvs/keywords.html
This has been used in the context of string replace. For ex, if you have scenario with a variable which takes different value in same string, you can use this as follows:
import string
mytext = "$dog is an animal"
replaceDogtoCat = {"dog":"cat"}
mytemplate = string.Template(mytext)
print mytemplate.substitute(replaceDogtoCat) #output: cat is an animal
replaceDogtoGoat = {"dog":"goat"}
print mytemplate.substitute(replaceDogtoGoat) #output: goat is an animal
$dog is a variable which would get replaced when substitute gets executed

Resources