Naming words inside square brackets in Dictionaries and Lists - list

We are having a naming issue. if we have dictionary:
dictionary[key]
or a list
list[1]
list[3:4]
list[:3]
what are the names of the words inside "[" "]", accessors? parameters? keys?

Regarding a dictionary, the name given to the value within the square brackets is a key. This is because a Dictionary is a key-value pair repository. An example of this use can be found in Microsoft's definition of a Dictionary item property here.
For a list, the name given to the value within the square brackets is an index. This is because it is used to select an item from the list at the specified (usually zero-based) index. Again, an example of this naming convention can be found here.
Note that whilst I have included links to Microsoft documentation in this answer, the use of "key" and "index" for dictionaries and lists (respectively) is a convention used through most, if not all, programming languages.

Related

code for .xsl bibliography style numeric in square brackets, sorted alphabetically

I want to Create a Custom bibliography style in MSWORD 2016 for Mac, I use either the style IEEE_Reference.XSL or ISO690_numerical.XSL with square brackets (I have already modified this one for square brackets).
But 1) I need that my references are sorted in alphabetical order (not according to siting) and 2) sorted according the language of references.
What changes can be applied to the codes of one of above mentioned styles to get the references sorted in alphabetical order?
Alternatively there is also a gost-r-7-0-5-2008-numeric-alphabetical.csl style, but it is .csl file and I do not know if one can convert it to .xsl
Can anyone help with this issue?
Thank you

Clojure - Quoting a List Adds "quote" text to list - How Do I Remove This?

I have the scenario where I have a map that has a field that is going to be a series of nested lists to create a tree like structure. I've quoted the lists with ' so that it works, without that I get errors as it tries to execute it.
(def test-map
{:tree '("1" "2" "3" '("1" "2"))})
The problem is this seems to add the word "quote" to the actual list.
(:tree test-map)
;("1" "2" "3" (quote ("1" "2")))
(first (last (:tree test-map)))
;quote
My question is how can I get rid of this "quote" text. My goal is to be able to work through these types of list by a certain number and be able to output the strings in sequence at a given depth.
(for [text (second (last (:tree test-map)))]
text)
This is a basic idea, but it feels weird having to use second here when visually looking at it the list I'm working through is the first item.
Is there a way to do what I want with lists? Is it a better idea to use vectors?
Just don't quote the inner list. Quotation applies to an entire nested structure, so if you wish to build a particular data structure you can just put a quote at the front and then write out the data structure normally.

Search and retrieve patterns from a list

Let's say I have a list of patterns such like ['AB', ')', '%%', '<.*>'].
I need to search for one of them forward or backward, starting from the cursor position.
Once the first one is found, how do I retrieve its index in the list? I.e, how do I know which one it is?
[EDIT]: the thing is that I actually have two lists of the same size. Once the first match is found in one direction, I'll need to search the corresponding one in the other direction.
PLUS, each pattern is associated with a certain precedence (its index in the list), which I need to retrieve once it is found.
(The overall idea is to build something that would be able to answer this question, with custom delimiters and operators.)
Got it: the searchpos function with the 'p' flag allows you to retrieve the position and the id of the match in for a compound pattern, see :help searchpos.

How to use environments for lookups

My question builds upon the topic of matching a string against multiple patterns. One solution discussed here is to use sapply(keywords, grepl, strings, ignore.case=TRUE) which yields a two-dimensional matrix.
However, I run into significant speed issues, when applying this approach to 5K+ keywords and 60K+ strings..(I cancelled the process after 12hrs).
One idea is to use hash tables, or environments in R. However, I don't get how "translate/convert" my strings into an environment while keeping the numerical index?
I have strings[1]... till strings[60000]
e <- new.env(hash=TRUE)
for (i in 1:length(strings)) {
assign(x=i, value=strings, envir=e)
}
As x in assign must be a character, I can't use it like this, but I hope you get my idea..I want to be able to index the environment with the same numbers like in my string[...] vector
Thanks for your help!
R environments are not used as much as perl hashes are, I think
just because there are not widely understood 'idioms' for doing
so. In your case the key question is, do you really want the
numerical index? If so it should be the value. The key is your
string, that's the whole point of the exercise.
e <- new.env(hash=T)
strings <- as.character(chickwts$feed) # note! not unique
sapply(1:length(strings), function(i)assign(strings[i], i, e))
e$horsebean # returns 10
In this example only the last index associated with each string
is kept, but you can assign anything that might be useful to each
key, such as a vector of indices.
You can then lookup your data in a number of ways. You can regex search
for keys using ls, for example, and retrieve the values using mget():
# find all keys containing 'beans'
ls(e, patt='bean')
# retrieve bean data
mget(ls(e, pat='bean'),e)

name splitting regex

I'm trying to split a string (a persons name) into components: prefix (Dr, Mr, Miss, etc), given, middle, family, and suffix (Jr, III, etc...).
Prefixes and suffixes can be a known list of options.
Edge cases for double barreled family names like 'da Vinci' or 'di Caprio' don't really bother me too much. The da's and di's will just be dropped in the middle name, or if a middle is given (i.e. 4 names are found that don't match a prefix or suffix) then everything after the second name is dropped in the family name.
I'm thinking about writing the regex myself... but before I go and reinvent the wheel, I wonder if anyone has something that works I can use?
Thanks.
Here is a proposal in perl (I did not find a language or regex flavor requirement).
Perl supports non-capturing groups, e.g. "(?:\w+)", which I consider needed to stay below 10 captured groups.
I am using "\w+" almost everywhere, for simplicity. Names can therefor contain "_" and digits. If you do not like that, use "[[:alpha:]]+" instead.
perl -pe"s/(?:(Dr\.|Mr\.) )?(?:(\w+)(?: (\w+(?: \w+)*))? )?(?:(\w+) (Jr\.|I+))|(?:(Dr\.|Mr\.) )?(?:(\w+)(?: (\w+(?: \w+)*))? )?(\w+)/pre\1\6 give\2\7 middle\3\8 fam\4\9 post\5/"
For demonstration purposes, the code replaces, while inserting field names.
Please extract the requested regex and fill in the missing pres and posts.
What I consider the trick is to have one big alternative "|", which prefers matches with a postfix.
The fields are filled by using two groups each, one from the first, one from the second alternative. Only one of each pair is non-empty.
I tested with a test text file, containing combination of
prefix present
postfix present
given present
middle present (assuming that more middles work too)
second middle present
All test cases have a family name.
"Superman II" and "Madonna" would both only have a family name, hope that is OK, the super hero movie gets a suffix.
"Dr. Who" has a prefix and a family name.
I.e. I ignored the "Di"s, as you permitted.
I consider the output plausible.