Terraform - How to join elements of a nested list? - list

I have two lists - ["l","h"], ["a","b"] and from that, I need to create a list like: ["la", "lb", "ha", "hb"] - is it possible?
I tried with setproduct(), flatten() and join() but the closest I could get, like this:
> setproduct(["l","h"], ["a","b"])
[
[
"l",
"a",
],
[
"l",
"b",
],
[
"h",
"a",
],
[
"h",
"b",
],
]
#
> flatten(setproduct(["l","h"], ["a","b"]))
[
"l",
"a",
"l",
"b",
"h",
"a",
"h",
"b",
]
I can also join a single element:
> join("",setproduct(["l","h"], ["a","b"])[1])
lb
but yet to figure out how to get ["la", "lb", "ha", "hb"] out of that. Any help from anyone?
-S

Using chunklist, flattern, join, and for loop,
> [for test in chunklist(flatten(setproduct(["l","h"], ["a","b"])), 2): join("", test)]
[
"la",
"lb",
"ha",
"hb",
]

A simpler way to do this is just two nested for expressions:
In the console:
> flatten([for i in ["l","h"]: [for j in ["a","b"]: "${i}${j}"]])
[
"la",
"lb",
"ha",
"hb",
]
In HCL:
output "flat" {
value = flatten([
for i in ["l","h"]: [
for j in ["a","b"]: "${i}${j}"
]])
}
Outputs:
Outputs:
flat = [
"la",
"lb",
"ha",
"hb",
]

Related

Python - how to remove nested key

a={
"car": "Nissan",
"from": [
{
"Japan1": "People1",
"make_type": "allow",
"driver": {
"id": "12345",
"name": "user1",
"type": "user"
}
},
{
"Japan1": "People1",
"make_type": "allow",
"driver": {
"id": "98765",
"name": "user2",
"type": "user"
}
}
]}
Objective: want to remove "name" and "type" inside "driver"
I have tried a lot of method, if use print the name or type, just put into a new var level by level.
But how can I delete it?
Thanks
You can use del:
for f in a["from"]:
del f["driver"]["name"]
del f["driver"]["type"]
print(a)
Prints:
{
"car": "Nissan",
"from": [
{"Japan1": "Peop1", "make_type": "plastic", "driver": {"id": "12345"}}
],
}
Or:
for f in a["from"]:
f["driver"] = {
k: v for k, v in f["driver"].items() if k not in {"name", "type"}
}

Match by whitespace indentation in textmate language grammar

I have a DSL that I'm trying to write a textmate grammar for, so I can have syntax highlighting (in VS Code rather than textmate, but it uses their grammar)
The DSL represent tree nodes, is whitespace indented, and gets mapped to JSON
Most of it is pretty straightforward, but it has some weird constructs that I'm not sure how to match with the textmate grammar. An example of the DSL is below.
I can match the "tags" and simple scalar type "properties" and the single line "arrays" just fine, but I'm a bit lost on the multiline arrays and objects - I don't know how to stop matching their contents when the indentation changes. I thought I could just stop matching when I hit a known pattern, but its ambiguous between a single item on the last line of the array and a "tag" apart from their indentation level
Is there some way to match these based on indentation alone?
Example DSL file:
fooTag
foo An unquoted string
bar 42
baz "42"
qux true
barTag
qux{}
foo A string called foo on the qux object
bar null
foo[] in arrays each space separated token is a separate string "unless quoted"
bar[]
some arrays of space separated tokens look like this when it is a long
list with lots of items
baz[]
arrays can be mixed types like this one which contains the number 42 and
the constants true false and null and the rest of the tokens are all
strings and even nested arrays and objects as follows
[] 1 2 3
[]
indented array of tokens
{}
foo 42
bar "42"
some
more
tags
Example JSON output:
[
{
"name": "fooTag",
"model": {
"foo": "An unquoted string",
"bar": 42,
"baz": "42",
"qux": true
}
},
[
{
"name": "barTag",
"model": {
"qux": {
"foo": "A string called foo on the qux object",
"bar": null
},
"foo": [
"each", "space", "separated", "token", "is", "a", "separate",
"string", "unless quoted"
],
"bar": [
"some", "arrays", "of", "space", "separated", "tokens", "look",
"like", "this", "when", "it", "is", "a", "long", "list", "with",
"lots", "of", "items"
],
"baz": [
"arrays", "can", "be", "mixed", "types", "like", "this", "one",
"which", "contains", "the", "number", 42 "and", "the", "constants",
true, false, "and", null, "and", "the", "rest", "of", "the", "tokens",
"are", "all", "strings", "and", "even", "nested", "arrays", "and",
"objects", "as", "follows",
[ 1, 2, 3 ],
[ "indented", "array", "of", "tokens" ],
{
"foo": 42,
"bar": "42"
}
]
}
},
[
{
"name": "some",
"model": {}
}
],
[
{
"name": "more",
"model": {}
}
],
[
{
"name": "tags",
"model": {}
}
]
]
]

C++ Boost Library - trying to get proper format of my property tree

I am trying to write Json using property tree. This is what I have tried:
boost::property_tree::propTree propTree1, propTree2, propTree3, propTree4, propTree5, propTree6, propTree7, propTree8, propTree9, propTree10;
propTree1.put( "a", "" );
propTree1.put( "b", "" );
propTree1.put( "c", "" );
propTree2.push_back(std::make_pair("", propTree1));
propTree3.put("l", "");
propTree3.put("m", "");
propTree4.push_back(std::make_pair("", propTree3));
propTree1.add_child("Msg", propTree4);
propTree5.put("r", "");
propTree5.put("s", "");
propTree6.push_back(std::make_pair("", propTree5));
propTree1.add_child("Msg1", propTree6);
propTree1.add_child("Msg2", propTree8);
propTree9.put("t", "");
propTree9.put("u", "");
propTree10.push_back(std::make_pair("", propTree9));
propTree1.add_child("Msg3", propTree10);
boost::property_tree::write_json( "OUTPUT", propTree1 );
The Output I am getting is below:
{
"a": "",
"b": "",
"Msg": [
{
"l": "",
"m": ""
}
],
"Msg1": [
{
"r": "",
"s": ""
}
],
"Msg2": "",
"Msg3": [
{
"t": "",
"u": ""
}
]
}
Expected output is below: (Commented are the questions)
{ "Msg0":{ //Missing in my output
"a": "",
"b": "",
"Msg": [
{
"l": "",
"m": ""
}
],
"Msg1": [
{
"r": "",
"s": "",
"abc": [ // Missing this "abc" array within Msg1 array
"note1", //Output with values (no keys like "r" & "s")
"note2", // Can we pass setlist/stringstream in "abc" array?
"note3",
]
}
],
"Msg2": "",
"Msg3": [
{
"t": "",
"u": ""
}
]
}
}
1) I am not able to get the format of JSON which is mentioned above. Please check the comments.
2) How should I add string/stringstream/setofnames in "abc" array? Which data type can be passed here and how to achieve that?
3) Also is there any efficient way of doing the above work?
Can anyone help me with the above 3 questions? Thanks.

How to match array of sub string with array of string using mongo?

I have follwoing collection structure -
{
"_id": ObjectId("54c784d71e14acf9ae833f9f"),
"vms": [
{
"name": "ABC",
"ids": [
"abc.60a980004270457730244662385a4f69",
"abc.60a980004270457730244662385a4f6d"
]
},
{
"name": "PQR",
"ids": [
"abc.6d867d9c7acd60001aed76eb2c70bd53",
"abc.60a980004270457730244662385a4f6d"
]
},
{
"name": "XYZ",
"ids": [
"abc.600605b00237d91016cdc38f376bd31d",
"abc.600605b00237d91016cdc38f376cd32f"
]
}
]
}
I have an array which contains substrings of ids. here is an array for your reference -
myArray = [ "4270457730244662385a4f69","4270457730244662385a4f6d" , "4270457730244662385a4f6b"]
I want to find each element of myArray is not present in ids as a substring using mongo.
Currently I am able to find single element using regex in mongo.
In above example, I want output as:
[
{
"name": "XYZ",
"ids": [
"abc.600605b00237d91016cdc38f376bd31d",
"abc.600605b00237d91016cdc38f376cd32f"
]
}
]
How do I find substring in array using mongo??
It is possible to do it using regex. You can match the string for multiple substrings using or operator. It is | in regex. Search for 'Boolean "or"' on wikipedia
MongoDB query using aggregation:
db.collection_name.aggregate([
{$unwind: "$vms"},
{$match: {
"vms.ids": {$not: /.*(4270457730244662385a4f69|4270457730244662385a4f6d|4270457730244662385a4f6b).*/}}
}
])
Output will be
{
"_id" : ObjectId("54c784d71e14acf9ae833f9f"),
"vms" : {
"name" : "XYZ",
"ids" : [
"abc.600605b00237d91016cdc38f376bd31d",
"abc.600605b00237d91016cdc38f376cd32f"
]
}
}

To split a json file.. Extracting data between curly braces

I have a json file. I want to split that file into different parts..
Following is my file's content.
I want to split the content based on the curly brackets {},
"1010320": {
"abc": [
"1012220",
"hiiiiiiiii."
],
"xyz": "Describe"
},
"1012757": {
"pqr": [
"1013757",
"x"
]
},
"1014220": {
"abc": [
"1018420",
"sooooo"
],
"answer": "4th"
},
"1019660": {
"abc": [
"1031920",
"welcome"
],
"xyz": "Describing&Interpreting"
},
"1034280": {
"abc": [
"1040560",
"Ok..."
],
"nop": "Student Question"
},
The output should be:
1) "abc": [
"1012220",
"hiiiiiiiii."
],
"xyz": "Describe"
2) "pqr": [
"1013757",
"x"
]
3) "abc": [
"1018420",
"sooooo"
],
"answer": "4th"
plz.. help..
i think this will be useful for you
(?<=\{)\n\s+((?:[\n]+|.*)+?)\n\}
regex demo here : http://regex101.com/r/rS3wI5