How to create an associative array from a range in D - range

Let's say I have an array of type Record[] and I want to create an associative array from it, key being rec.key. Is there an easy way to do it?

Yes, you can use std.array, std.typecons and std.algorithm libraries and construct this one-liner:
Record[Key] assocArray = array.map!( item => tuple( item.key, item ) ).assocArray;
It takes array, maps it to a tuple (Key, Record) and then takes that list of tuples and creates an associative array from it.

Related

Gets elements from 3 dimensional array

I'm new in Swift and do not know everything , i have this kind of an array
var arrayOFJumpForwardBack: [Int: [String: [String]]] =
[1: ["chapter": ["15 sec","30 sec","45 sec","60 sec"]]]
and , i'd like to know how can i get certain elements from this array?
Swift - Arrays & Dictionaries
The syntax of an array is as follows:
["value1","value2","value3"]
The syntax of a dictionary is as follows:
["ABC":"valueForABC","XYZ":"valueForXYZ"]
In this case you're using a dictionary, and the main difference with the array is that the order doesn't matter, each value is represented with a key, so with the following example:
Example:
Let's say we have a dictionary where we have stored the word "apple" whose key is "JHC" and "pear" whose key is "IOP". We want to print "I have an apple", then:
var myFridge:[String:String] = ["JHC":"apple","IOP":"pear"]
print("I have an \(myFridge["JHC"]!)")
A multidimensional array is just an array inside of another, x dimensions you want.
I'd rather prever to create a dictionary as above ↑

coffee script - remove from one array based on second array contents

I have 2 arrays - one has attribute called vcdName and the other has attribute called name. I want to remove from array 1 all entries where I find the value of vcdName in the second array. So:
array one
data...vcdName=a
data...vcdName=b
array two
data...name=a
I want to filter array one and remove the first entry (because vcdName value a is found in array two).
I understand I can use lodash functions and I tried the following. I believeusing is will return me those entries that match and I need to put in filteredArray those entries where no match is found in arraytwo.
filteredArray = _.filter(#arrayone, (vcd) -> vcd.vcdName is #arraytwo)
I hope I have not gotten too confusing. It feels this should be moderately easier than I am making it.
Generically I need to do this
for each entry in arrayone
for each entry in arraytwo
does entry from arrayone.vcdName == arraytwo.name - if yes then I do not want that entry from arrayone
I hope that helps
filter is in js core so I don't see why you'd need to use the lodash function, but anyway you can do it like this:
arraytwo_names = #arraytwo.reduce (dict, entry) ->
dict[entry.name] = true
dict
, {}
results = #arrayone.filter (x) ->
!arraytwo_names[x.vcdName]
The reduce is not strictly speaking necessary, since you could loop through arraytwo each iteration of the filter, but it is an optimization and makes the runtime O(N) and not O(N^2).

Scala create array of empty arrays

I am trying to create an array where each element is an empty array.
I have tried this:
var result = Array.fill[Array[Int]](Array.empty[Int])
After looking here How to create and use a multi-dimensional array in Scala?, I also tried this:
var result = Array.ofDim[Array[Int]](Array.empty[Int])
However, none of these work.
How can I create an array of empty arrays?
You are misunderstanding Array.ofDim here. It creates a multidimensional array given the dimensions and the type of value to hold.
To create an array of 100 arrays, each of which is empty (0 elements) and would hold Ints, you need only to specify those dimensions as parameters to the ofDim function.
val result = Array.ofDim[Int](100, 0)
Array.fill takes two params: The first is the length, the second the value to fill the array with, more precisely the second parameter is an element computation that will be invoked multiple times to obtain the array elements (Thanks to #alexey-romanov for pointing this out). However, in your case it results always in the same value, the empty array.
Array.fill[Array[Int]](length)(Array.empty)
Consider also Array.tabulate as follows,
val result = Array.tabulate(100)(_ => Array[Int]())
where the lambda function is applied 100 times and for each it delivers an empty array.

select items from a struct array matlab- based on condition

I have a struct array named Lst. Every struct has the following form:
Point (x,y)
Type (1-6)
I want get the separate array of points for each type. How can I get it?
Lst(Lst.Type==1);
won't work since Type is not a field of Lst but of Lst(i).
In addition, is there a way to save the indexes of each item or an alternative way to then combine them again to the original order?
L1 = Lst([Lst.Type]==1); will give you the subset L1 of Lst where Type == 1.
Likewise, you can use idx1 = find([Lst.Type]==1) to memorize your indexes.
EDIT: the above uses the [] operator to aggregate the field elements Type of Lst into an array. To your comment/question, you could use the exact same operator also to obtain an array of specific field elements X of a subset of the structured array, as in
X1 = [Lst([Lst.Type]==1).X];

What does Array as Object mean in Basic4Android?

SQL1.ExecNonQuery2("INSERT INTO table1 VALUES(?,?,?)",Array As Object("def",3,4))
I don't seem to understand why the argument list in the above statement is declared in the form of Array as Object('xx','xx''xx').How is it exactly being converted into a list parameter ?
Array As xxx is a shorthand syntax for declaring a new array and assigning the values.
Array As Object("def", 3, 4)
Is equivalent to:
Dim arr As Object(3)
arr(0) = "def" : arr(1) = 3 : arr(1) = 4
Basic4android automatically wraps arrays as lists when needed. The items are not copied, it is the whole array that is wrapped in a list. Therefore the above code is valid as it creates an array which is then wrapped as a List.