How to access map in template string? - dictionary

I want to use values from gradle.properties that should go into a template string.
A naive first:
println("${project.properties[somekey]}")
doesn't work: Unresolved reference: somekey
So, quotes required?
println("${project.properties[\"somekey\"]}")
is completely broken syntax: Expecting an expression for the first .
I couldn't find any example how to do this, yet the official documentation says expressions.
Question: is it possible to access a map in string template, and if so, how?

Yes and as follows:
"${project.properties["someKey"]}"
assuming the Map has the following signature: Map<String, Any?> (or Map<Any...)
Alternatives:
"${project.properties.getValue("someKey")}"
"${project.properties.getOrElse("someKey") { "lazy-evaluation-default-value" }}"
"${project.properties.getOrDefault("someKey", "someFixedDefaultValue")}"
Basically all the code you put in the ${} is just plain Kotlin code... no further quoting/escaping required, except for the dollar sign $ itself, e.g. use "\$test" if you do not want it to be substituted with a variable named test or """${"$"}test""" if you use a raw string
Note that in this println case the following would have sufficed as well (which also goes for all the shown alternatives above. You may omit the outer surrounding quotes and ${} altogether):
println(project.properties["someKey"])
See also Basic types - String templates

Related

Why fn:substring-after Xquery function could not be used inside ML TDE

In my ML db, we have documents with distributor code like 'DIST:5012' (DIST:XXXX) XXXX is a four-digit number.
currently, in my TDE, the below code works well.
However instead of concat all the raw distributor codes, I want to simply concat the number part only. I used the fn:substring-after XQuery function. However, it won't work. It won't show that distributorCode column in the SQL View anymore. (Below code does not work.)
What is wrong? How to fix that?
Both fn:substring-after and fn:string-join is in TDE Dialect page.
https://docs.marklogic.com/9.0/guide/app-dev/TDE#id_99178
substring-after() expects a single string as input, not a sequence of strings.
To demonstrate, this will not work:
let $dist := ("DIST:5012", "DIST:5013")
return substring-after($dist, "DIST:")
This will:
for $dist in ("DIST:5012", "DIST:5013")
return substring-after($dist, "DIST:")
I need to double check what XPath expressions will work in a DTE, you might be able to change it to apply the substring-after() function in the last step:
fn:string-join( distributors/distributor/urn/substring-after(., 'DIST:'), ';')

Copy dictionary to register in Vim

I have a dictionary in my vimrc that I would like to copy and paste into whatever text file I'm editing:
let info = { 'tom':12345, 'bob':54689 }
I tried using put as I would with an option setting but I get an error:
:put=#info
How do I copy the contents of "info" to the register so that I can paste it in my text document?
If you want to maintain the exact representation, you have to store the dictionary as a string already (as per #sergio's answer), and you can :put it directly.
If it's fine to have Vim render the dictionary (and potentially reorder its elements), you just have to explicitly convert to a string, in order to overcome the E731: using Dictionary as a String (as it's :help explains, these types are not automatically converted). The string() function will do that:
:put =string(info)
Try quoting the json data. And removing # from put command (# is used to accessed registers, not for variables):
:let info="{ 'tom':12345, 'bob':54689 }"
:put=info

Do I need an empty sequence () in addition to a comment in an enclosed expression?

When trying to use the oXygen editor to comment out a node inside of an element oXygen simply wrapped it into (:<foo>foo 1</foo>:), but I then found out that that way the node did not get commented out but was rather prefixed by a text node with (: and suffixed by a text node with :).
Then I looked up the syntax and found out you need to use an enclosed expression {(:<foo>foo 1</foo>:)} instead to have access to the comment syntax.
However, while BaseX and Saxon 9.8 happily accept {(:<foo>foo 1</foo>:)}, Altova complains and needs an additional empty sequence {(:<foo>foo 1</foo>:)()}.
https://www.w3.org/TR/xquery-31/#doc-xquery31-EnclosedExpr suggests in XQuery 3.1 the expression inside curly braces is optional and defaults to ().
Does this also mean that in XQuery 3.1 it should suffice to use simply the comment inside of the curly braces, without an empty sequence?
So to summarize, Saxon and BaseX allow me to use <root>{(:<foo>foo 1</foo>:)}</root> while Altova complains about incorrect syntax, forcing me to use <root>{(:<foo>foo 1</foo>:)()}</root>.
Is that still necessary in XQuery 3.1?
Sounds like a bug in their commenter, which is pretty common in XQuery editors. Within in an element - and assuming you are using direct element constructors, not computed element constructors - use XML comments:
<hello>world
<!-- Don't print me -->
</hello>
Computed element constructors still use XQuery comments:
element hello {
'world' (: Don't print me :)
}

Pyparsing setResultsName dynamically

I'm using PyParsing to parse a log file. The elements I want have the regular left aligned structure
<key>:<value>
I can easily get the key parsed but crucially want to use this as arg to setResultsName. I think I've just satisfied myself that pp.Forward() is not the answer.
Can anyone provide one? Key point is the need to use as arg to setResultsName a string which becomes known only as the file is parsed.

Is it possible to disable Command Substitution in Bash?

Is it possible to disable Command Substitution in Bash?
I want to pass a string containing several backticks characters as command-line argument to a program, without trailing backslashs or quoting the string.
Thank you.
I assume there is a misconception which grounds your question. Quoting is most likely the solution to your situation. But maybe you haven't found the right way of quoting yet or similar.
If your dangerous string shall be verbatim (without quoting or escaping) in the source code, you can put it in a separate file and read it from there:
dangerous_string=$(cat dangerous_string_file.txt)
If it shall be passed without interpretation to a command, use the double quotes to prevent interpretation:
my_command "$dangerous_string"
If you have to pass it to a command which needs to receive a quoted version of your string because it is known to carelessly pass the string without using sth like the double quotes to prevent interpretation, you can always use printf to get a quoted version:
quoted_dangerous_string=$(printf "%q" "$dangerous_string")
careless_command "$quoted_dangerous_string"
If all these options do not help in your situation, please explain in more detail where your problem lies.

Resources