Results 1 to 8 of 8

Thread: .NET Spliting Into A 2-Dim Array

  1. #1
    Registered+
    Join Date
    Aug 2005
    Location
    Liverpool/Brum Uni
    Posts
    18
    Thanks
    0
    Thanked
    0 times in 0 posts

    .NET Spliting Into A 2-Dim Array

    Basically the text file holds entrant details for a Micro Mouse Competition. There are 23 details for each entrant, which are separated using TAB as the delimiter. At the moment the text file holds 10 entrants, each on its own line.

    I’m trying to get this data to split into a 2 dim array to build a table of the entrants. Is it possible to split straight in to 2 dim array, or is it necessary to split into single dim arrays and parse together to make the 2dim array?

    Cheers For anyhelp you can give,

    Matt.

  2. #2
    Treasure Hunter extraordinaire herulach's Avatar
    Join Date
    Apr 2005
    Location
    Bolton
    Posts
    5,618
    Thanks
    18
    Thanked
    172 times in 159 posts
    • herulach's system
      • Motherboard:
      • MSI Z97 MPower
      • CPU:
      • i7 4790K
      • Memory:
      • 8GB Vengeance LP
      • Storage:
      • 1TB WD Blue + 250GB 840 EVo
      • Graphics card(s):
      • 2* Palit GTX 970 Jetstream
      • PSU:
      • EVGA Supernova G2 850W
      • Case:
      • CM HAF Stacker 935, 2*360 Rad WC Loop w/EK blocks.
      • Operating System:
      • Windows 8.1
      • Monitor(s):
      • Crossover 290HD & LG L1980Q
      • Internet:
      • 120mb Virgin Media
    Surely you can just read it in in a loop, which language in .net are you using? i imagine something along the lines of

    while not EOF
    while not EOL
    array[a,b] = spam
    b+1
    wend
    a+1
    read next line
    wend

    should work ok. Thats how id do it anyway, that or set a proper database backend up.

  3. #3
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    you REALLY shouldn't use a regular 2D array for this in .net

    you should instead use the DataSet class, its in effect a type of multi-dimensional array, which is typed.

    The great thing about this is, it would let you use XML easily too or interface to a database. Also you can make parsers for text. Its far better than just a regular array.
    throw new ArgumentException (String, String, Exception)

  4. #4
    Comfortably Numb directhex's Avatar
    Join Date
    Jul 2003
    Location
    /dev/urandom
    Posts
    17,074
    Thanks
    228
    Thanked
    1,027 times in 678 posts
    • directhex's system
      • Motherboard:
      • Asus ROG Strix B550-I Gaming
      • CPU:
      • Ryzen 5900x
      • Memory:
      • 64GB G.Skill Trident Z RGB
      • Storage:
      • 2TB Seagate Firecuda 520
      • Graphics card(s):
      • EVGA GeForce RTX 3080 XC3 Ultra
      • PSU:
      • EVGA SuperNOVA 850W G3
      • Case:
      • NZXT H210i
      • Operating System:
      • Ubuntu 20.04, Windows 10
      • Monitor(s):
      • LG 34GN850
      • Internet:
      • FIOS
    I can't see any way to do this all in 1 go, since you need to read the input file a line at a time. Use System.String.Split( '\t' ) on each line, and add to the end of your array. Something like this:

    Code:
    StreamReader myfile = new StreamReader( Path.Combine( Path.Combine( Path.Combine( "path", "to" ), "your" ), "file" );
    string line;
    string[][] myarray;
    int i = 0;
    while( ( line = myfile.ReadLine( ) ) != null )
       {
          myarray[i] = line.Split( '\t' );
          i++;
       }
    N.B. No I haven't tested & debugged the above. infact, this looks a lot like a 1D array of 1D arrays, rather than a 2D array

  5. #5
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    Well, you *could* do it like that...

    But really this is what the IFormatProvider interface is for:

    http://msdn2.microsoft.com/en-us/lib...tprovider.aspx

    You may end up doing a similar thing inside a class that implements IFormatProvider, but at least you'll have a nice reusable interface to the code
    Simon


  6. #6
    Seething Cauldron of Hatred TheAnimus's Avatar
    Join Date
    Aug 2005
    Posts
    17,168
    Thanks
    803
    Thanked
    2,152 times in 1,408 posts
    totally disagree, this IS NOT a format issue, as we aren't dealing with representation of single values.

    we have comma seperate values.

    as such the "most elegent" solution would be an IDataAdapter for handling , seperated, with a typed dataset.

    http://msdn2.microsoft.com/en-us/lib...r_members.aspx
    throw new ArgumentException (String, String, Exception)

  7. #7
    Sublime HEXUS.net
    Join Date
    Jul 2003
    Location
    The Void.. Floating
    Posts
    11,819
    Thanks
    213
    Thanked
    233 times in 160 posts
    • Stoo's system
      • Motherboard:
      • Mac Pro
      • CPU:
      • 2*Xeon 5450 @ 2.8GHz, 12MB Cache
      • Memory:
      • 32GB 1600MHz FBDIMM
      • Storage:
      • ~ 2.5TB + 4TB external array
      • Graphics card(s):
      • ATI Radeon HD 4870
      • Case:
      • Mac Pro
      • Operating System:
      • OS X 10.7
      • Monitor(s):
      • 24" Samsung 244T Black
      • Internet:
      • Zen Max Pro
    What is this system for?

    How is the data brought into the system, and what do you need to do with it?
    Last edited by Stoo; 19-05-2006 at 09:54 PM.
    (\__/)
    (='.'=)
    (")_(")

  8. #8
    Senior Member Shad's Avatar
    Join Date
    Jul 2003
    Location
    In front
    Posts
    2,782
    Thanks
    23
    Thanked
    42 times in 25 posts
    I would combine the two really. No reason why you can't implement an IFormatProvider to interpret each individual line into a DataRow. By doing that inside the IDataAdapter you then have the ability to later handle single lines of text, matching the initial format.
    Simon


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. .NET Txt File Line counter
    By Shadow_101 in forum Software
    Replies: 5
    Last Post: 18-04-2006, 04:11 PM
  2. Cloning a Raid0 to a Raid5 array
    By Stephen B in forum Software
    Replies: 5
    Last Post: 29-06-2005, 09:35 AM
  3. Broken RAID Array
    By Nemeliza in forum Help! Quick Relief From Tech Headaches
    Replies: 5
    Last Post: 13-09-2004, 03:44 PM
  4. RAID array falling apart!
    By Steve in forum PC Hardware and Components
    Replies: 0
    Last Post: 03-05-2004, 10:30 AM
  5. VB6 - accessing array data publically
    By Steve in forum Software
    Replies: 4
    Last Post: 26-04-2004, 10:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •