# How do I use a value stored in a variable to access an element in an array?

**URL:** <https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372>\
**Category:** DQL\
**Tags:** dql, dynatrace-managed\
**Created:** [April 25, 2025, 12:15pm UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372 "2025-04-25T12:15:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![badgerfifteen](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@badgerfifteen](https://community.dynatrace.com/u/badgerfifteen)\
**Post date:** [April 25, 2025, 12:15pm UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372/1 "2025-04-25T12:15:54Z")

</div>

Within my environment, I have hosts with various tags against them. I’m looking for a tag which contains:

```markup
DEPARTMENT:

```

and this tag can be in index 5 or 4, it is random.

Here is my code so far:

```markup
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]

```

```markup
| fieldsAdd buIndex = contains(tags[],"DEPARTMENT:")

```

The code above checks each array element and return true if it matches the above.

The next piece of code then the element where ‘true’ was found including its index.

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?

---

<div class="post-metadata">

**Author:** ![PacoPorro](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pacoporro/32/36087_2.png) [@PacoPorro](https://community.dynatrace.com/u/PacoPorro)\
**Post date:** [April 25, 2025, 12:40pm UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372/2 "2025-04-25T12:40:36Z")

</div>

Do you know expand can be used to separate the tags? [https://docs.dynatrace.com/docs/shortlink/structuring-commands#expand](https://docs.dynatrace.com/docs/shortlink/structuring-commands#expand)

---

<div class="post-metadata">

**Author:** ![badgerfifteen](https://avatars.discourse-cdn.com/v4/letter/b/e68b1a/32.png) [@badgerfifteen](https://community.dynatrace.com/u/badgerfifteen)\
**Post date:** [April 25, 2025, 1:55pm UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372/3 "2025-04-25T13:55:26Z")

</div>

How would I utilise it to grab a specific tag if its located in different array elements?

---

<div class="post-metadata">

**Author:** ![krzysztof\_hoja](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/krzysztof_hoja/32/34625_2.png) [@krzysztof\_hoja](https://community.dynatrace.com/u/krzysztof_hoja)\
**Post date:** [April 27, 2025, 7:41pm UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372/4 "2025-04-27T19:41:03Z")

</div>

You can use iterative expressions to interact with arrays (fields tags is array of strings):

First I parse tags into array of records containing context, tag and its value:

```markup
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 (!<<'\\' ':') 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]))))

```

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.

---

<div class="post-metadata">

**Author:** ![PacoPorro](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/pacoporro/32/36087_2.png) [@PacoPorro](https://community.dynatrace.com/u/PacoPorro)\
**Post date:** [April 28, 2025, 6:25am UTC](https://community.dynatrace.com/t/how-do-i-use-a-value-stored-in-a-variable-to-access-an-element-in-an-array/51372/5 "2025-04-28T06:25:43Z")

</div>

Expand tags and filter tags containing Department&nbsp; ?
