# DQL Matching and comparing on two arrays

**URL:** <https://community.dynatrace.com/t/dql-matching-and-comparing-on-two-arrays/48595>\
**Category:** DQL\
**Tags:** dql\
**Created:** [October 28, 2024, 8:17pm UTC](https://community.dynatrace.com/t/dql-matching-and-comparing-on-two-arrays/48595 "2024-10-28T20:17:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![calfano](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/calfano/32/35558_2.png) [@calfano](https://community.dynatrace.com/u/calfano)\
**Post date:** [October 28, 2024, 8:17pm UTC](https://community.dynatrace.com/t/dql-matching-and-comparing-on-two-arrays/48595/1 "2024-10-28T20:17:24Z")

</div>

Hello DQL Experts,

Running into a situation where I have two arrays. I want to be able to make records based on the comparison of these two arrays without using the&nbsp; **expand or summarize** &nbsp;commands.

I can’t really articulate it well so here’s an example of what I’m looking for.

array1 = [“cat”, “cat”, “dog”, “dog”, “dog”, “snake”]

array2 = [“cat”, “dog”, “snake”]

Desired Output:  
totals = { “cat”:2, “dog”:3, “snake”:1 }

Any help is greatly appreciated 🙂

---

<div class="post-metadata">

**Author:** ![GerardJ](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/gerardj/32/154_2.png) [@GerardJ](https://community.dynatrace.com/u/GerardJ)\
**Post date:** [October 29, 2024, 8:06am UTC](https://community.dynatrace.com/t/dql-matching-and-comparing-on-two-arrays/48595/2 "2024-10-29T08:06:12Z")

</div>

Hello&nbsp;[@calfano](https://community.dynatrace.com/user/58627)

Assuming that you want to count in array1 the number of occurence of values in array2, I’d do something like this :

```markup
data record()
| fieldsAdd array1 = Array("cat", "dog", "snake", "dog", "dog", "snake")
| fieldsAdd array2 = Array("cat", "dog", "snake", "cow")
| fieldsAdd array1 = arraySort(array1, direction:"ascending")
| fieldsAdd result = iCollectArray(concat(array2[],":",
        if(arrayLastIndexOf(array1, array2[])>=0, arrayLastIndexOf(array1, array2[])-arrayIndexOf(array1,array2[])+1, 
          else:arrayLastIndexOf(array1, array2[]))))

```

![GerardJ_0-1730189113409.png](https://global.discourse-cdn.com/dynatrace/original/3X/7/b/7b1d870813942b6fe80eaf6dd757120bde64edf8.png)

---

<div class="post-metadata">

**Author:** ![calfano](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/calfano/32/35558_2.png) [@calfano](https://community.dynatrace.com/u/calfano)\
**Post date:** [October 29, 2024, 12:48pm UTC](https://community.dynatrace.com/t/dql-matching-and-comparing-on-two-arrays/48595/3 "2024-10-29T12:48:24Z")

</div>

WOW That’s it! Thank you so much. This is very impressive!
