Matching arrays

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dumuz

Is there a way to match an array, even if it’s not in a specific order?
Just as long as the variables are in there? Or is there a better method to do that?

For example:

func _my_func():
     var potential_array: []
     # statements that add to 'potential_array' based off of circumstances.

func _matching(new_array):
     match new_array: #hypothetically its ["test1","test4","test3","test5"]
               #Not in the same order, but I'd like for it to simply check all the variables and match anyways.

ANY help would be appreciated, to keep me from writing every single possible outcome order.
I’m still new to this.

:bust_in_silhouette: Reply From: jgodfrey

Something like this should work:

func arrays_have_same_content(array1, array2):
	if array1.size() != array2.size(): return false
	for item in array1:
		if !array2.has(item): return false
		if array1.count(item) != array2.count(item): return false
	return true

Pass in 2 arrays, and get back either True if the arrays have the same items or False if they do not…

EDITED to handle differing numbers of duplicated elements (mentioned below)

As an explanation, it basically does this:

  • Verify that both arrays have the same number of elements. If they do not, they can’t have the same elements, so return false
  • Otherwise, iterate through every element in array1. If array2 does not have that element, return false.
  • Next, verify that both arrays contain the same number of the current element (in case the arrays contain duplicate elements). If they don’t have the same number, return false.
  • Finally, if we made it through all the elements without having returned a false along the way, both arrays have the same elements, so return true

jgodfrey | 2020-02-17 19:23

Note, I’ve modified the posted script so the below “issue” is properly handled, So, the below comments are no longer accurate…

Also, note this is only 100% accurate if each array contains unique items. If you pass in 2 arrays that contain the same number of elements, but different quantities of common elements, it’ll get fooled.

For instance, it’ll return true on these 2 arrays…

var a1 = [1, 1, 1, 2]
var a2 = [2, 2, 2, 1]

Because they both have 4 elements and the elements consist of only the integer values 1 and 2. It will not detect the differing quantities of duplicated elements.

But, if you don’t have duplicate items in your arrays, you should be fine.

If you do, the algorithm would need to be improved to account for that.

jgodfrey | 2020-02-17 19:30

Note, I just modified the originally posted function to handle the “differing number of duplicate elements” issue I mentioned in my previous comment. So, it should work for that case now too…

jgodfrey | 2020-02-17 19:57

Jgodfrey, thank you for time, effort and consideration put into answering my question. You opening my mind to the idea of using the Array.has(variant) and comparing the array.size, is exactly what I needed to work around having to match all the variables in order.

Thanks again!

Dumuz | 2020-02-18 13:25

One follow up question: Is there a way to extract original text from a string?
I’d like to pass a string to another script, but for that script to take the name in the string as if it were one of it’s global variables.


Script 1:

func set_parameters():
     var outcome
     if test = true:
            outcome = "alive"
          outcome = "dead"

Script 2:

var alive = "I live!"
var dead = "I'm not alive!"

func _testing_for(incoming):

This way I don’t have to assign each possible string to a variable.

Dumuz | 2020-02-18 14:59

I’m not sure how this relates to the original question. Assuming it doesn’t, you’re probably better off asking a completely new question (with an appropriate title, …). That way, someone with the same question in the future will be able to find an answer.

If it does relate, I’m just missing it. Feel free to add another comment…

jgodfrey | 2020-02-18 15:18

Gotcha, better for the community, good looking out.

Posted new question here.

Dumuz | 2020-02-18 15:41