I have 2 variables json array
variable1 =
[
{
"CurrentDate": "2019-05-11",
"CurrentTime": "01:09:14",
"tablename": "A"
},
{
"CurrentDate": "2019-05-11",
"CurrentTime": "01:11:14",
"tablename": "Z"
}
]
variable2 =
[
{
"A": "Topic1"
},
{
"Z": "Topic2"
}
]
Now I want to compare tablename value with variable2 key and if it matches pass variable2 value to next step.
For example, in variable1 for each loop it will take tablename value A checks with variable2 array each element and key matches for A then I need to pass its value as Topic1 to next step or assign a value to global variable. Same happens for all the elements one by one.
I tried using 2 foreach but unable to assign values. How can I write an expression to match value and assign global value after match to variable?
If this is not possible I will modify variable2 like
variable2 =
[
"A" , "B"
] and add equal expression like "equals": [
"#items('For_each')?['tablename']",
"#items('For_each_2')"
]
You can now run code inside a logic app (limited to javascript for the moment):
See the documentation for more information:
Add and run code snippets by using inline code in Azure Logic Apps.
This should do the trick for you.
Related
I am looking for a terraform expression to retrieve values from a list, i have a list of values
namespaces = [blue,red,green,ns-blue,ns-green,ns-grey]
I would like to retrieve in list format just the values contains "ns", as a result i must get:
namepsace-filtred = [ns-blue,ns-green,ns-grey]
thanks in advance.
Assuming you have a list of strings for a variable namespace:
variable "namespaces" {
default = ["blue", "red", "green", "ns-blue", "ns-green", "ns-grey"]
}
You can use a for with the regex function to check if a string contains a substring. Also, you have to use the can function to transform the result of the regex to a boolean:
locals {
namepsace_filtred = [for ns in var.namespaces : ns if can(regex("ns", ns))]
}
The result of this should be something like this:
namepsace_filtred = [
"ns-blue",
"ns-green",
"ns-grey",
]
Given this JSON hash:
{
"id": 55555,
"name": "111111",
"custom_data_field": [
{
"id": 1,
"label": "Vehicle Type",
"value": "Coach"
},
{
"id": 2,
"label": "Vendor",
"value": 1
}
]
}
I need the value associated with each label.
I'm able to get the value using the array's index:
#item()?['custom_data_field'][0]['value'] # Coach
#item()?['custom_data_field'][1]['value'] # 1
But this is a bit fragile.
This syntax doesn't work:
#item()?['custom_data_field'][#label=='Vehicle Type']['value'] # Coach
#item()?['custom_data_field'][#label=='Vendor']['value'] # 1
Is there a way to do this reliably?
According to the description of your question, it seems the data {"id": 55555, "name": "111111"....} you provided is an item of a array list because your expression begins with item() (I guess you use this expression in a "For each" or something else loop action). And custom_data_field is array under the item, you want to do filter/select operation to get the value which its label equals Vehicle Type just by the expression. I don't think we can do it just in one expression (because the label and value are not key-value map, we can not do the filter/select in expression easily).
To meet the requirement, we need to use a more sophisticated approach such as "Filter array" action mentioned by Scott in comments. We need to set the array custom_data_field to the input box("From" box) of the "Filter array" action.
And then add the filter condition.
After running the logic app, it will filter the items by the filter condition.
As the filter action don't know how many items match the condition, so the output will always be a array but not an item or a record even if there is only one item matches the condition(label equals "Vehicle Type") in your custom_data_field list.
So if you want to get the value, you need to get it by writing an expression as below screenshot.
Hope it helps~
In Terraform, I want a user to enter values for a variable (type list) without having to worry too much about the syntax for a list variable. For example, Terraform requires the following syntax for lists:
Enter value: ["value1", "value2", "value3"]
It would be nice if the user just needed to enter a comma separated list without having to worry about adding quotations and the brackets. For example:
Enter value: value1, value2, value3
From the comma separated string, I would like to convert it to type list with the correct syntax.
My current code looks like this, I don't think I am even close to figuring it out. Any help is appreciated!
variable "subnetNames" {
description = "Enter value:"
default = "value1, value2, value3"
}
output "test" {
value = "${join(",", list("[", var.subnetNames, "]"))}"
}
You want to use the split function.
variable "subnetNames" {
default = "value1,value2,value3"
}
output "test" {
value = "${split(",", var.subnetNames)}"
}
$ terraform init && terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
test = [
value1,
value2,
value3
]
I've "inherited" some Tcl code, and while I worked through some tutorials and can make sense out of the language, my own Tcl constructs lack a certain... finesse.
For example, I have this code:
puts "Column 'name': [ $queryRs getString name ]"
$queryRs is the result set of a SQL query. The [ $queryRs getString name ] construct retrieves the content of the table column "name" from the current row in the result set. If the database field is NULL, the puts does not print anything.
I would like to print a "default" string instead, i.e. if [ $queryRs getString name ] results in nothing, I'd like to replace it with "--".
Now, I could do something like this:
set nameVar "[ $queryRs getString name ]"
if { [ string length $nameVar ] == 0 } {
set nameVar "--"
}
puts "Column 'name': $nameVar"
But there has to be a more compact solution, something that can be done inline instead of adding four lines and a temporary variable. Help, please?
Two things.
First, Tcl doesn't have a notion of NULL (nil, undefined or whatever) value, and when you want to simulate such a value you have to either use a variable and test for its existence or use an entry in an array of dictionary and test for its existence, too. If such a variable/entry exists, then the value is defined, otherwise it's not.
Unfortunately, the creator of your inherited code apparently did not care about the case where a variable can be NULL so NULLs are indistinguishable from variables having default values (an empty string).
Next, you can use a helper procedure to do what you need:
proc ValueOrDef {val {def --}} {
expr {$val ne "" ? $val : $def}
}
and then go like this:
puts [ValueOrDef [$queryRs getString name]]
puts [ValueOrDef [$queryRs getString name] "some other default"]
You could use the x?y:z construct of the expr command. x is the condition, y is the alternative if the condition is met and z is the alternative if x is not met.
E.g. (but still with a temporary variable):
set nameVar [ $queryRs getString name ]
puts "Column 'name': [expr {[string length $nameVar]>0 ? $nameVar : "--"}]"
In this code snippet, fields-types in the end is modified by the to-camel-case function, vs. being passed as a local variable to the parent function:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
to-camel-case: function [name] [
name/1: lowercase name/1
replace/all name space ""
]
fill-template-body: func [
field-labels-types [block!] /local vars fields-names-types
] [
vars: [member-name member-type]
field-names-types: copy []
foreach [field-label field-type] field-labels-types [
append field-names-types to-camel-case field-label
append field-names-types field-type
]
]
fill-template-body fields-types
Execution gives:
>> fill-template-body fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>> fields-types
== ["firstName" "string" "lastName" "string" "age" "int"]
>>
Whereas I would want that fields-types to stay invariant:
fields-types: ["First Name" "string" "Last Name" "string" "Age" "int"]
Of course I can try to circumvent this by modifying to-camel-case to use a copy of name, but that is not something I think I should have to do.
Is there something like the var and val keywords in Scala?
Variable is an ugly word in REBOL, everything - even words - are values. This isn't some semantic newspeak, it's helpful in understanding the way in which REBOL flows.
I think of values as being contained within one giant array in memory, where REBOL (the language) uses words and their contexts to reference and interact with the values. Most REBOL functions operate without duplicating these values:
head lowercase next uppercase str: "abcd"
remove back tail str
This is one of REBOL's most efficient features - you don't need copies for intermediary processes, to require such is wasteful. Think of that array growing where every time you use replace, uppercase or to-camel-case a value is duplicated. Whole processes can be built on the premise of modification rather than duplication - indeed a context can be built without necessarily having to return a value:
remove-spaces: use [space mark][
space: charset " ^-"
[any [mark: space (remove mark) | skip]]
]
parse/all str: "Should Be No Spaces" remove-spaces
The trick then becomes knowing where to copy values, and I think happens to intersect with REBOL's gift for concise expression:
parse/all link: copy title: "An Article on REBOL" remove-spaces
print ["(" link ")" title]
to-camel-case copy field-label
And of course, modification has its limitations. Sometimes it is cleaner building from scratch.
Your camel case function operates on the original value so if you want to preserve the original value, you need to copy it, and return the altered value. Since your function acts on a template it needs to copy the template right??
So, something like this should work:
fill-template-body: func[ labels [block!] /local field-labels-types vars fields-names-types][
field-labels-types: copy labels
..