|
|
|
|
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!
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.
Example;
Script 1:
func set_parameters():
var outcome
if test = true:
outcome = "alive"
else:
outcome = "dead"
$other_script._testing_for(outcome)
Script 2:
var alive = "I live!"
var dead = "I'm not alive!"
func _testing_for(incoming):
print(incoming)
This way I don’t have to assign each possible string to a variable.
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.