Find string in text file always returns false

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

I’m trying to check a text file to find if a string exists in it but always returns false.

func check_word():
var txt = load_file(file_path)
var wordlist = txt.split("\n")
print(picked_word)
for i in range(wordlist.size()):
	if wordlist[i] == picked_word:
		print("correct")
	else:
		print("false")

I’m checking the “picked_word” and it isn’t empty or has spaces. I also checked some words in the file(e.g wordlist[1], wordlist[43], and I’m getting the words.
Do I miss something?

Maybe you have whitespace in your file?

exuin | 2021-02-27 14:10

I don’t think so, because I read this same file with the same way to get a random word without problems.
I also test it by printing 2 words of a file and they printed the one next of the other without whitespace(like one word).

dancaer69 | 2021-02-27 14:18

Are you sure picked_word is in the file? Does Array.find() work?

exuin | 2021-02-27 14:31

Array.find() not working(nonexistent function error)
The picked_word exists, and also I tried with a string instead with a word from the file.
This method run on _input method when the mouse button released. The “false” message printed a lot of times, so is possible that there’ s no time to search all the file(has 25000 words) and for this never finds it?

dancaer69 | 2021-02-27 14:40

You use Array.find() like this: wordlist.find(picked_word).

I think the method has time to search through the whole array. It’s not going to quit the function just because it ran out of time.

Can you send me the text file or at least a sample of it?

exuin | 2021-02-27 14:44

I tried this and I get the error:
nonexistent function in base poolStringArray.
But, I was using the wrong file. Sorry for the false alert.

dancaer69 | 2021-02-27 15:02

Oh alright. I guess that function only works with Arrays and not PoolStringArrays then.

exuin | 2021-02-27 15:23

:bust_in_silhouette: Reply From: Wakatta

No need for a for-loop just do

if picked_word in wordlist:
    print("correct")
else:
    print("false")

Also string comparisons are case sensitive So
“Foo” == “foo” false
“FooBar” == “Foobar” false