Copy dictionary to register in Vim - dictionary

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

Related

How to access map in template string?

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

Create JSON to send API request in R

This might have been asked and solved before, I just can't get a straightforward answer.
I got the following:
text <- 'Testing to be translated'
Which I am trying to get into JSON format like:
[{"Text": "Testing to be translated"}]
I have tried using toJSON but I could not get that structure.
Additionally, I did some quick-fix:
paste0('[{"Text":"', text, '"}]')
Which would work fine; however, I have some strings with the " and ' characters in it and they would break this code.
Any input would be helpful.
More context: I am using a GET request to translate text from Azure server, could not use translateR so I am creating my own function.
To create an array, pass jsonlite::toJSON an unnamed list or vector. You should also set auto_unbox=TRUE so that scalars aren't treated as arrays.
text <- 'Testing to be translated'
jsonlite::toJSON(list(list(Text=text)), auto_unbox=TRUE)
# [{"Text":"Testing to be translated"}]

filename expansion on assigning a non-array variable

This is about Zsh 5.5.1.
Say I have a glob pattern which expands to exactly one file, and I would like to assign this file to a variable. This works:
# N: No error if no files match. D: Match dot files. Y1: Expand to exactly one entry.
myfile=(*(NDY1))
and echo $myfile will show the file (or directory). But this one does not work:
myfile=*(NDY1)
In the latter case, echo $myfile holds the pattern, i.e. *(NDY1).
Of course I could do some cheap trick, such as creating a chilprocess via
myfile=$(echo *(NDY1))
but is there a way to do the assinment without such tricks?
By default, zsh does not do filename expansion in scalar assignment, but the option GLOB_ASSIGN could help. (This option is provided as for backwards compatibility only.)
local myfile=''
() {
setopt localoptions globassign
myfile=*(NDY1)
}
echo $myfile
;#>> something
Here are some descriptions in zsh docs:
The value of a scalar parameter may also be assigned by writing:
name=value
In scalar assignment, value is expanded as a single string, in which the elements of arrays are joined together; filename expansion is not performed unless the option GLOB_ASSIGN is set.
--- zshparam(1), Description, zsh parameters
GLOB_ASSIGN <C>
If this option is set, filename generation (globbing) is performed on the right hand side of scalar parameter assignments of the form 'name=pattern (e.g. foo=*'). If the result has more than one word the parameter will become an array with those words as arguments. This option is provided for backwards compatibility only: globbing is always performed on the right hand side of array assignments of the form name=(value) (e.g. foo=(*)) and this form is recommended for clarity; with this option set, it is not possible to predict whether the result will be an array or a scalar.
--- zshoptions(1), GLOB_ASSIGN, Expansion and Globbing, Description Of Options, zsh options

outputting text in XQuery using saxon9he: how do I get "<" and ">" created in the output themselves, not escaped?

My XQuery script:
declare namespace output = "http://​www.​w3.​org/​2010/​xslt-​xquery-​ser​iali​zati​on";
declare option output:method "text";
for $row in all/row
return ('"<row>","',data($row),'"
')
My XML:
<all>
<row>one</row>
<row>two</row>
<row>three</row>
</all>
My command line:
java -cp …/saxon9he.jar net.sf.saxon.Query '!omit-xml-declaration=yes' -s:./trouble-with-output-escaping.xml -q:./trouble-with-output-escaping.xqy
My output as created by saxon9he:
"<row>"," one "
"<row>"," two "
"<row>"," three "
I actually want to have output like this:
"<row>","one"
"<row>","two"
"<row>","three"
During my web investigation I came across XSLT's disable-output-escaping.
I thought: if XQuery had that, that might help.
Update/0:
Actually nothing (visible) was wrong with the above XQuery script.
The namespace declaration above needs to get replaced by this one:
declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";
Looks the same, but it isn't, as Michael pointed out.
Having completed this, the above is an example of how to create text output using XQuery.
In some other place Michael showed, how get rid of the space (0x20), that is being used to separate the lines, i.e. the space character preceding lines 2 to the end:
string-join(…,"")
where "…" would be the entire FLWOR.
It's doing the right thing if you set output method "text" from the command line, that is
java net.sf.saxon.Query -q:test.xquery -s:test.xml -t !method=text
but you had me baffled as to why setting the serialization options from within the query isn't working. Looking at it in the debugger, though, I see that your URI, which looks like
http://​www.​w3.​org/​2010/​xslt-​xquery-​ser​iali​zati​on
actually contains several occurrences of decimal 8203, hex 200B, which is a zero-width space. This means the URI doesn't match the serialization output URI, and "declare option" with an unrecognized URI is ignored.

Programmatically getting a list of variables

Is it possible to get a list of declared variables with a VimL (aka VimScript) expression? I'd like to get the same set of values that will be presented for a command using -complete=expression. The goal is to augment that list for use in a user-defined command completion function.
You can use g: as a dictionary that holds all global variables, so:
let globals = keys(g:)
will give you all the names. The same applies to the other scopes: b:, s:, w:, etc. See :help internal-variables for the complete list.
You can get something similar using keys of g:, b:, t:, w: and v: dictionaries, but beware of the following facts:
There is no equivalent to this dictionaries if you want to complete options.
Some variables like count (but not g:count or l:count), b:changedtick and, maybe, others are not present in this dictionaries.
Some vim hacker may add key ### to dictionary g:, but it won't make expression g:### valid variable name (but adding 000 there will). Though g:["###"] will be a valid expression.

Resources