Search Unity

Help with Arraylist.contains

Discussion in 'Scripting' started by Darloc, Jun 8, 2012.

  1. Darloc

    Darloc

    Joined:
    Feb 16, 2011
    Posts:
    167
    I'm not sure how to do this I'm trying to see if a word is contained in my arraylist here is what I have

    Code (csharp):
    1. var dataArray = new ArrayList();
    2. var currentWord : String = "";
    3. var temp = dataFile.text.Split("\n"[0]);
    4.    
    5.     for(var j = 0; j < temp.length;j++ )
    6.     {
    7.         dataArray.Add(temp[j]);
    8.     }
    9. Debug.Log(dataArray.Contains(currentWord));
    no matter what string I put for currentWord I always get back false even if I know it is in the list

    Thanks for any advise I am not familiar with ArrayList
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    It is possible that your problem does not result from the ArrayList as much as it probably does the operating system.

    Windows systems use a combination Carriage return Line Feed as an indicator of a new line. In thier eyes you line fed, to get tot eh new line and carriage returned to get your typewriter back to the first column of letters. (yes, typewriters)

    In Unix, They just said, Hey, all we need is Line Feed.

    At Apple they just use the Carriage return.

    CR == ACSii(13)
    LF == ASCii(10)

    Now, for what we are looking at, you may have a CR (or /n) and/or you may have a LF (or /r)

    The way we could fix it to make sure is to do some replacements.

    Code (csharp):
    1.  
    2. var temp : string = dataFile.text;
    3. if(temp.IndexOf("\r") > 0){ // only do this if we have lf's
    4. temp.Replace("\r", "\n");// replace all the lf's with cr's to fix one part
    5. temp.Replace("\n\n", "\n");// replace the double cr's to fix the windows part
    6. }
    7.  
    If we did it without checking to see if we had lf's then we would remove any double return. (could be bad)
     
  3. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,518
    A few things.

    I would strongly suggest changing dataArray to a List<string> so its type safe.
    Secondly, you can use System.Environment.NewLine to capture the discrepancies BigMisterB describes above.
     
  4. Darloc

    Darloc

    Joined:
    Feb 16, 2011
    Posts:
    167
    That didn't seem to do it, let me know if I integrated it correctly

    Code (csharp):
    1. var temp : String = dataFile.text;
    2.     if(temp.IndexOf("\r") > 0) // only do this if we have lf's
    3.     {
    4.  
    5.     temp.Replace("\r", "\n");// replace all the lf's with cr's to fix one part
    6.  
    7.     temp.Replace("\n\n", "\n");// replace the double cr's to fix the windows part
    8.  
    9.     }
    10.  
    11.     var temp2 = temp.Split("\n"[0]);
    12.    
    13.     for(var j = 0; j < temp2.length;j++ )
    14.     {
    15.         dataArray.Add(temp2[j]);
    16.     }
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The only problem that I can see with that is if you have platform independent text. Say you build your game on a PC, run the stand alone, then build it for Mac, and transfer a file between the two. It will not automatically fix that by using the NewLine reference. ;)
     
  6. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,518
    Indeed - hadn't thought of that.
     
unityunity