- Home /
Splitting textasset into an array and binary search
I'm making a word game, all words are stored in a SORTED txt file in my resources folder. I've learned the only way to be platform independent is to use the resources.load method. However, when I split my text asset into an array I am having trouble searching the array. Here's my code`
TextAsset asset = (TextAsset)Resources.Load("words");
string[] words = asset.text.Split('\n')`
Now if I print out random positions in the array the words print out as expected, so they are all properly loaded up. There are no extra characters. But for some reason when I do a binary search on it it never finds my word.
int result = Array.BinarySearch(words, "hello");
This will return a negative value indicating it couldn't be found, even if I print out the exact location that I know where it is in the array and it prints out "hello". To my eye they always look identical and this is driving me nuts. Could it have something to do with text encoding? It was working no problem when I did some file IO and split the file like string[] words = System.IO.File.ReadAllLines("Path");
But if I do that I have to figure out the file path for each platform(this is mobile iOS, Android, shortly Windows phone).
I'm at a loss here....any help would be appreciated or other ideas. Thanks!
Steve
You may have carriage feeds and line feeds in the file which would result in a non-match. You don't have to load the TextAsset from Resources. Just place it in Assets and then drag and drop it onto the 'asset' in the Inspector.
Sorry I'm a newb, not sure what you mean. Where am I dragging and dropping it? And I should remove it from the resources folder and just put it in the assets folder? Then how do I access it programmatically? Thanks
Ok thanks I'll give it a try. I'm a little skepticle, however. This solution seems like I'm just moving things around but in the end I'm still just loading a text asset and splitting it, with the only change being two delimiters ins$$anonymous$$d of one. But I'm so annoyed I'd jump off a bridge if someone thought it would solve my issue.
What I suspect is that your strings in the file look like:
"hello\\r\\n"
So after the split they will look like:
"hello\\r"
And the '\\r' is preventing the match
The rest of the moving around has nothing to do with your issues. I just wanted to make a point that you don't have to load something from Resources to be platform independent...that you can directly link in the text asset to your script file at edit time.
Answer by robertbu · Jun 09, 2013 at 01:46 AM
Move the TextAsset out of Resources to anywhere in Assets (including a subfolder you create). At the top of the class you will put:
public TextAsset asset;
Your script above will be attached to a game object. Select the game object in the Hierarchy. Then drag and drop the text asset on to the asset variable (where it says 'None (TextAsset)').
As for splitting the lines, I am inexperienced with C# text manipulation, but I would do it this way:
char[] archDelim = new char[] { '\r', '\n' };
words = asset.text.Split(archDelim, StringSplitOptions.RemoveEmptyEntries);
I think you need to do a 'using System.Text;' in order to get 'StringSplitOptions'.
Thank you sooooooo much!!!!! It was indeed the carriage returns. Unreal, I never would have figured that out on my own. Awesome
Steve
Glad it worked. I converted my comment to an answer. Please click the checkmark to close out the question.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Double Parse a text file into 2d array or jagged array 1 Answer
Distribute terrain in zones 3 Answers
Brute Force Search Algorithm 0 Answers
Racing Game Leaderboard with Photon 0 Answers