I do a foreach on the above
Then split on “,” and I now need to extract from the split (regexmacth) value starting with string1a (for example)
Then a further split on this extracted string using the “|” delimter
I am trying all methods of madness but none seem to work
here is the latest one i am trying
for_each = { for i, v in local.listmap : i => v }
…
…
somevar = split(“,”,each.value)[index([for v in split(“,”,each.value) : v], regex(“config.*”,v))]
For a problem like this I’d typically start by trying to project the input into a more reasonably-shaped data structure. Using a better-suited data structure will then make the rest of the problem easier to describe.
You didn’t share any code so I’m going to assume the following input variable representing the input data you shared:
locals {
transformed = tolist(concat([
for s in var.input : [
for seq in split(",", s) :
split("|", seq)
]
]...))
}
You could then use local.transformed for your subsequent filtering and transformation steps. For example, you could apply your regex pattern to the first element of each of the nested lists to select only the pairs whose first element matches the pattern.
Thank you for the fast feedback greatly appreciated, for better context the input derives from data external (runs a PS Script) - the json output limitation of data external I ended up with this source data.
Each value in the list will be fed into a template file i.e. each string in the list will be template file 0,1,2 and so on…
Then within each value in the list contains the values I need to feed in as variables delimited by first “,” then “|” (the template file creates a dashboard in azure - json file - and will contain panels for various metrics contained in the string value delimited by “,” and “|”.
I will look at transformed way above and feedback thank you once again
I am still exploring this and making some changes but running into an issue where I cannot nest any further in the transform…
I now have this as my source data
I am still relatively new to HCL however there are some corner cases where I find HCL very limited and spend a huge amount of time trying to workaround limitations which should not exist (i.e. if data external could handle valid but complex json outputs I would not of spent 3 days so far trying to fix / workaround the limitations)
HCL is a configuration language - or, the framework from which applications build a configuration language by adding application-specific behaviour.
When you say HCL, I think you more properly mean Terraform.
The fact that terraform-provider-external’s data source requires a strict map of strings is indeed a limitation, that comes from Terraform’s approach of applying schema to data source / resource inputs and outputs.
If this is a problem for you, I strongly suggest returning a complex JSON object as a single string value within the map from the external script, and then applying jsondecode() to it within a Terraform expression.
This would be a significantly easier workaround than the complex structures you’re currently developing within this topic.