Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Splitting a String Results in Less Pieces?

Discussion in 'Scripting' started by Studio_Akiba, Jan 11, 2019.

  1. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    So, a bit of backstory.

    I'm trying to populate a 34 x 26 (884 tile) grid using a text array (a series of characters in a text file representing that grid), as a way of building levels.

    The file has numbers (currently all 0s with a 1 at the very beginning and end), seperated by 3 spaces (just for visual purposes) each.

    When I split the file I only get 859 elements in the array I'm populating.

    What exactly have I done wrong? I'm sure it's something stupid and simple but I just can't see it, I'm hoping someone here who is better at programming can work out what I have done wrong.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class GameGridMapper : MonoBehaviour
    8. {
    9.  
    10.     public TextAsset asciiArray;
    11.     public string[] arrayContents;
    12.  
    13.     private GridLayoutGroup gameGrid;
    14.     private Image tilePanel;
    15.     public int gridSize;
    16.  
    17.     void Start()
    18.     {
    19.         BuildGrid();
    20.     }
    21.  
    22.     public bool BuildGrid()
    23.     {
    24.         //Standard grid: 34l_x_26h
    25.  
    26.         gameGrid = FindObjectOfType<GridLayoutGroup>();
    27.         tilePanel = gameGrid.gameObject.GetComponentInChildren<Image>();
    28.         tilePanel.gameObject.SetActive(false);
    29.  
    30.         string array = asciiArray.text;
    31.         arrayContents = array.Split(new string[] { "   " }, StringSplitOptions.None);
    32.  
    33.         StartCoroutine(PopulateGrid());
    34.  
    35.         return true;
    36.     }
    37.  
    38.     IEnumerator PopulateGrid()
    39.     {
    40.         if (gridSize < 884)
    41.         {
    42.             Image newTile = Instantiate(tilePanel, gameGrid.transform);
    43.             newTile.gameObject.SetActive(true);
    44.             gridSize++;
    45.             newTile.name = "Tile " + gridSize;
    46.             yield return new WaitForSeconds(0.0001f);
    47.             StartCoroutine(PopulateGrid());
    48.         }
    49.     }
    50. }
     

    Attached Files:

    • 1.txt
      File size:
      3.4 KB
      Views:
      550
  2. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Do you have newlines in the text that might be tricking your split into have elements like "0001\n0001"?
     
  3. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    That looks fine to me... any chance of seeing the actual data you're parsing? My guess would be that some of those " " are actually " " or " ", and it's just hard to tell by looking at it.
     
  4. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,350
    looks like the text file only has 33 spacing per row, so 33*26=858
     
  5. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    The issue is your newlines. Notice the result of parsing this:

    upload_2019-1-11_13-11-47.png

    So, you can either first merge all the lines together on " ", or process your text one line at a time.
     
  6. Studio_Akiba

    Studio_Akiba

    Joined:
    Mar 3, 2014
    Posts:
    1,421
    I've changed my code to this:
    Code (CSharp):
    1. string array = asciiArray.text;
    2.         array.Replace("\n", "   ");
    3.         arrayContents = array.Split(new string[] { "   " }, StringSplitOptions.None);
    which I thought might replace the new lines with three spaces, but I think it's looking for the characters \n, not a new line.
     
  7. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,193
    It's easier just to split on whitespace instead of messing with the newlines explicitly:

    Code (CSharp):
    1. var array = Regex.Split(input, "\\s+");
    Regex.Split(input, "\\s+");

    That will split on any whitespace (spaces, tabs, newlines), which also conveniently means if you ever put two spaces instead of 3, that'll still work.
     
    Kurt-Dekker likes this.