Skip to main content
marcot0005
April 11, 2022
Question

Get union of other fields

  • April 11, 2022
  • 2 replies
  • 0 views

So I have a CDT with a bunch of fields in it. One of the fields is a category, while the other is string. That string field will be identical throughout a particular category, but may or may not be different in different categories. So for example:

local!data: {{category:"round things", string:"bubbles, <otherdata>}, {category:"round things", string:"bubbles, <otherdata>}, {category:"soapy things", string:"bubbles, <otherdata>}, {category:"hard things", string:"rocks, <otherdata>}}

I'd like to get the union of categories for that set, but instead of the output being the categories, it is the strings. 

so something like union(local!data.category, local!data.category) and have the output be: {"bubbles, "bubbles", "rocks"} instead of {"round things", "soapy things", "hard things"}

I hope the idea is clear?

2 replies

peter.lewis
Participating Frequently
April 11, 2022

If the string data is guaranteed to be identical within a category, you could probably make it easy and just return the first item in each category list. Here's an example:

 

a!localVariables(
  local!data: local!data: {
    {
      category: "round things",
      string: "bubbles, "
    },
    {
      category: "round things",
      string: "bubbles, "
    },
    {
      category: "soapy things",
      string: "bubbles, "
    },
    {
      category: "hard things",
      string: "rocks, "
    }
  },
  local!uniqueCategories: union(touniformstring(local!data.category), touniformstring({})),
  a!forEach(
    items: local!uniqueCategories,
    expression: index(
      local!data.string,
      
      /* Only pulls back the first string from all the categories */
      index(
        
        /* Identifies the rows that match the same category */
        wherecontains(
          tostring(fv!item),
          touniformstring(local!data.category),
        ),
        1,
        {}
      ),
      {}
    )
  )
)

marcot0005
April 11, 2022

That sounds like what I need, perfect. Thank you!