I have two lists in terraform and want to get the intersection of these lists.
For example,
list1 = ["a", "b", "c"]
lists2 = ["b", "c", "d"]
I am looking to get output as ["b", "c"] using built-in terraform functions.
You are looking for something like this
output my_intersection {
value = setintersection( toset(var.list1), toset(var.list2) )
}
Related
I have a problem. I wanted to write a function that would compare 2 sheets and if they are equal, put them on the same position, otherwise put any handicap sign on that position, such as "-".
I was thinking something like this
let l1 = ["a", "b", "c"]
let l2 = ["a", "d", "c"]
(l1', l2') = myFunc l1 l2
l1' == ["a", "b", "c", "-"]
l2' == ["a", "-", "c", "d"]
I just don't understand what the algorithm should be, that's the point, maybe I could implement it in Haskell, but I think it would be very ugly
You can use the Diff package for this. Example:
Data.Algorithm.Diff> getDiff "abc" "adc"
[Both 'a' 'a',First 'b',Second 'd',Both 'c' 'c']
I have a list. I want to convert it to string. However, the results are a bit different from what I expected.
Expected result:
'["a", "b", "c", "d"]'
Actual result:
[a, b, c, d]
My code:
final List list = ["a", "b", "c", "d"];
print(list.toString());
How can I get my expected results? I would appreciate any help. Thank you in advance!
You could use the jsonEncode of it to get the desired result:
print(jsonEncode(list));
You can simply do this by using json.encode() function:
final list = ['a', 'b', 'c', 'd'];
final result = json.encode(list);
You can try jsonEncode:
final List list = ["a", "b", "c", "d"];
print(jsonEncode(list));
Output:
["a","b","c","d"]
for example if I have a list like this :
list =['a','a','a','b','b','b','c']
I want to know how many different elements are in my list and generate a list like this:
list1 = ['a','b','c']
set(list)
produces
>>> set(list)
{'b', 'a', 'c'}
If you then want it as a list you can use
list(set(list))
full_list = ["a", "b", "a", "c", "c"]
list_without_duplicaion = list(dict.fromkeys(full_list))
print(list_without_duplicaion)
Try out this answer and let me know is working as your expectations.
https://repl.it/#TamilselvanLaks/arrwithoutdup
I am trying to create a list of maps from two different lists in terraform 0.12
E.g.
List1: ["a", "b", "c"]
List2: ["aa", "bb", "cc"]
Output required:
[{
"list1element" = "a"
"list2element" = "aa"
}, {
"list1element" = "b"
"list2element" = "bb"
}, {
"list1element" = "c"
"list2element" = "cc"
}]
If I could get the index of the element in the loop this would be so easy. Nested loops also make no sense.
If you know that the two lists will always have the same length, you can use the indices from one list with the other list:
[for i, v in list1 : {
list1element = list1[i]
list2element = list2[i]
}]
Which is the correct way from the below snippets?
1) def list = ["a", "b", "c", "d"]
list.findAll({println(it)})
2) def list = ["a", "b", "c", "d"]
list.collect({println(it)})
3) def list = ["a", "b", "c", "d"]
list.grep({println(it)})
Please advice.
If all you are doing is printing elements, you should use each.
def list = ["a", "b", "c", "d"]
list.each { println(it) }
findAll and grep are used to find specific elements in a list. collect is used to build a new collection from the elements in the given list.
So the correct way to print all the elements is using each, which is exactly what your example is. This is also likely the choice for when you don't care about the result of the processing. If you only want to process part of the list, use findAll or grep, which can filter the list before running the closure. These, along with collect, all return a new list.