<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic How do I use a value stored in a variable to access an element in an array? in DQL</title>
    <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275936#M1994</link>
    <description>&lt;P&gt;Within my environment, I have hosts with various tags against them. I'm looking for a tag which contains:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEPARTMENT:&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;and this tag can be in index 5 or 4, it is random.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code so far:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;timeseries { percent = avg(dt.host.disk.used.percent), used = avg(dt.host.disk.used), avail = avg(dt.host.disk.avail) }, by: { dt.entity.host, dt.entity.disk }
| fieldsAdd Utilisation = arrayAvg(percent), entityName(dt.entity.disk)
| fieldsAdd entityName(dt.entity.host)
| lookup [ fetch dt.entity.host 
| fields id, tags[] ], sourceField:dt.entity.host, lookupField:id
| fieldsAdd tags = `lookup.tags[]`
| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")
| fieldsAdd test = arrayIndexOf(buIndex, TRUE)
| fieldsAdd BU = tags[test]&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")&lt;/LI-CODE&gt;&lt;P&gt;The code above checks each array element and return true if it matches the above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next piece of code then the element where 'true' was found including its index.&lt;/P&gt;&lt;P&gt;Where I have the field BU, is to access the index stored in test to give me the DEPARTMENT: text. However, I can't seem to figure it out. Any ideas?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 25 Apr 2025 12:15:54 GMT</pubDate>
    <dc:creator>badgerfifteen</dc:creator>
    <dc:date>2025-04-25T12:15:54Z</dc:date>
    <item>
      <title>How do I use a value stored in a variable to access an element in an array?</title>
      <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275936#M1994</link>
      <description>&lt;P&gt;Within my environment, I have hosts with various tags against them. I'm looking for a tag which contains:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;DEPARTMENT:&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;and this tag can be in index 5 or 4, it is random.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code so far:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;timeseries { percent = avg(dt.host.disk.used.percent), used = avg(dt.host.disk.used), avail = avg(dt.host.disk.avail) }, by: { dt.entity.host, dt.entity.disk }
| fieldsAdd Utilisation = arrayAvg(percent), entityName(dt.entity.disk)
| fieldsAdd entityName(dt.entity.host)
| lookup [ fetch dt.entity.host 
| fields id, tags[] ], sourceField:dt.entity.host, lookupField:id
| fieldsAdd tags = `lookup.tags[]`
| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")
| fieldsAdd test = arrayIndexOf(buIndex, TRUE)
| fieldsAdd BU = tags[test]&lt;/LI-CODE&gt;&lt;LI-CODE lang="markup"&gt;| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")&lt;/LI-CODE&gt;&lt;P&gt;The code above checks each array element and return true if it matches the above.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The next piece of code then the element where 'true' was found including its index.&lt;/P&gt;&lt;P&gt;Where I have the field BU, is to access the index stored in test to give me the DEPARTMENT: text. However, I can't seem to figure it out. Any ideas?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 12:15:54 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275936#M1994</guid>
      <dc:creator>badgerfifteen</dc:creator>
      <dc:date>2025-04-25T12:15:54Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a value stored in a variable to access an element in an array?</title>
      <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275948#M1995</link>
      <description>&lt;P&gt;Do you know expand can be used to separate the tags? &lt;A href="https://docs.dynatrace.com/docs/shortlink/structuring-commands#expand" target="_blank"&gt;https://docs.dynatrace.com/docs/shortlink/structuring-commands#expand&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 12:40:36 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275948#M1995</guid>
      <dc:creator>PacoPorro</dc:creator>
      <dc:date>2025-04-25T12:40:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a value stored in a variable to access an element in an array?</title>
      <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275959#M1998</link>
      <description>&lt;P&gt;How would I utilise it to grab a specific tag if its located in different array elements?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Apr 2025 13:55:26 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/275959#M1998</guid>
      <dc:creator>badgerfifteen</dc:creator>
      <dc:date>2025-04-25T13:55:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a value stored in a variable to access an element in an array?</title>
      <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/276012#M2004</link>
      <description>&lt;P&gt;You can use iterative expressions to interact with arrays (fields tags is array of strings):&lt;BR /&gt;&lt;BR /&gt;First I parse tags into array of records containing context, tag and its value:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;timeseries { percent = avg(dt.host.disk.used.percent), used = avg(dt.host.disk.used), avail = avg(dt.host.disk.avail) }, by: { dt.entity.host, dt.entity.disk }
| fieldsAdd Utilisation = arrayAvg(percent), entityName(dt.entity.disk)
| fieldsAdd entityName(dt.entity.host)
| fieldsAdd entityAttr(dt.entity.host, "tags")

| fieldsAdd ptags = parse(dt.entity.host.tags[], """('[' LD:context ']')?((LD:tag (!&amp;lt;&amp;lt;'\\' ':') LD:value)|LD:tag)""")
| fieldsAdd ptags = record(context=ptags[][context], tag=replaceString(ptags[][tag],"\\:",":"),  value=replaceString(ptags[][value],"\\:",":"))

| fieldsAdd department= arrayFirst(iCollectArray((if(ptags[][tag]=="DEPARTMENT", ptags[][value]))))&lt;/LI-CODE&gt;&lt;P&gt;and later I can pick any element of this array using any condition based on record (I filter by tag name only in this example.&lt;/P&gt;</description>
      <pubDate>Sun, 27 Apr 2025 19:41:03 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/276012#M2004</guid>
      <dc:creator>krzysztof_hoja</dc:creator>
      <dc:date>2025-04-27T19:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a value stored in a variable to access an element in an array?</title>
      <link>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/276023#M2005</link>
      <description>&lt;P&gt;Expand tags and filter tags containing Department&amp;nbsp; ?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Apr 2025 06:25:43 GMT</pubDate>
      <guid>https://community.dynatrace.com/t5/DQL/How-do-I-use-a-value-stored-in-a-variable-to-access-an-element/m-p/276023#M2005</guid>
      <dc:creator>PacoPorro</dc:creator>
      <dc:date>2025-04-28T06:25:43Z</dc:date>
    </item>
  </channel>
</rss>

