Search Unity

Does anyone know how to convert spreadsheets to arrays?

Discussion in 'Scripting' started by D3Jason1, Mar 22, 2013.

  1. D3Jason1

    D3Jason1

    Joined:
    Feb 18, 2012
    Posts:
    125
    Without the use of an SQL server, i'd like to accomplish the same goal of having an external DB instead of an unchangeable internal DB.
    my dad has a co worker programmer that was able to convert a CSV version of a spreadsheet into arrays which then are used to read song names and artists to his computer. He is much too busy to learn unity and explain this to me, so does anyone know?
     
  2. kablammyman

    kablammyman

    Joined:
    Nov 22, 2010
    Posts:
    507
    do you know what csv stands for? in other words, parsing a csv file is stupid easy. Dont over think it, and make it happen
     
  3. D3Jason1

    D3Jason1

    Joined:
    Feb 18, 2012
    Posts:
    125
    Know what it means, no idea how to do it.
     
    Last edited: Mar 22, 2013
  4. D3Jason1

    D3Jason1

    Joined:
    Feb 18, 2012
    Posts:
    125
    problem is i'm horrible with arrays. if i can get a script that turns a txt or csv file into a 4 column array with 100+ rows, and teach me how to use it to declare vars, i would appreciate it.
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  6. D3Jason1

    D3Jason1

    Joined:
    Feb 18, 2012
    Posts:
    125
    how do i do this inside unity? there doesn't seem to be a split command inside unity so...
     
  7. D3Jason1

    D3Jason1

    Joined:
    Feb 18, 2012
    Posts:
    125
    also i need to know how to form the array. and how to use arrays. I will have a text document that will read similar to
    1, song, artist, year
    2, song, artist, year
    3, song, artist, year
    etc.
    i need to be able to get the song/year/artist from any of those. no idea how it is done
     
  8. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Just did this for a data mining project:

    Code (csharp):
    1.  
    2.  
    3.   private List<List<String>> data = new List<List<string>>();
    4.  
    5.  private void ReadCSVFile()
    6.         {
    7.             string line;
    8.             FileStream file = new FileStream("Data_Mining_Student_DataSet_Spring_2013.csv", FileMode.Open);
    9.             StreamReader stream = new StreamReader(file);
    10.  
    11.             while ((line = stream.ReadLine()) != null)
    12.             {              
    13.                 string[] lines = line.Split(';');
    14.  
    15.                  if (data.Count < 1)
    16.                  {
    17.                      for (int i = 0; i < lines.Length; i++)
    18.                      {
    19.                             data.Add(new List<String>());
    20.                      }
    21.                   }
    22.  
    23.                     for (int i = 0; i < lines.Length; i++)
    24.                    {
    25.                         data[i].Add(lines[i]);
    26.                    }
    27.                 }
    28.             }
    29.         }
    30.  
    31.  
     
  9. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    The simplest solution is String.Split as BFGames demonstrates. However, be warned that this only works with a subset of CSV. The complete CSV format allows weird things like line breaks, commas and quotations that need to be handled carefully.

    There are also libraries out there to help you - e.g. http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader

    Too be honest though, if you're having difficulty parsing basic CSV I suggest spending more time in C# .Net tutorials, cause that's what they are there for.
     
  10. BFGames

    BFGames

    Joined:
    Oct 2, 2012
    Posts:
    1,543
    Regular Expressions are your friend ;)