How can i find in array combination of number and string?

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

I dont understand why i cand do this:

Var my_array = [numb1, 1, 2, 3, numb2]
For i in range(3):
   print("numb", i)

It would simply say “-1”, as if it cant find it.
Pls help!
Edit:
Instead of previously writtenprint("numb",i) i meant to write print(my_array.find("numb",i) and if someone looking for answer pls read SECOND comment of DDoop.

:bust_in_silhouette: Reply From: DDoop

I would expect that to print:
numb 0
numb 1
numb 2

If you want to print each item in the array, this for loop would work:

for i in range(my_array.size()):
     print("numb", my_array[i])

and it would print:
numb numb1
numb 1
numb 2
numb 3
numb numb2

Yeah, sorry, i would mean i need to fund index of “string+number” and I totally wrote wrong part of code. I meant to write this: print(my_array.find("numb",i))

GibberishDevelopment | 2020-07-01 22:26

That code makes much more sense relative to your question title.
That said, the reason for getting -1 out of my_array.find("numb", i) is that you are passing 2 separate objects into the function (I believe unnecessarily). Consider instead passing just one object, as I guess that is your intention: to find the index of any element that matches the literal object"numb1" or "numb2".
You could do that by changing your .find() call to my_array.find("numb" + i). Godot will resolve the + operator by combining the two objects into one before passing them into the function. When you separate 2 objects with a comma in a function call, Godot assumes you intend to pass 2 arguments. Reading the docs on Array.find here shows that the second argument you are passing is actually an optional argument, defaulting to 0, which determines where .find starts looking in the Array you are operating on.
In other words, you are currently asking Godot to find all instances of the literal string "numb" in your array. There are no elements in your array that are the literal string "numb", so you get -1 to tell you that.
EDIT: You may also need to “cast” i to a string type via str(i). I’m not sure if that happens implicitly.

DDoop | 2020-07-01 22:50

Thank you very much!

GibberishDevelopment | 2020-07-02 00:56