Another VB Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • crokett
    The Full Monte
    • Jan 2003
    • 10627
    • Mebane, NC, USA.
    • Ryobi BT3000

    #1

    Another VB Question

    Ok, so I am parsing a text file. Here is some of the data. I already have the code to pull this into a string array. Problem is this array can vary in size based on the size of the zoneset. Ideally I run through the file, count the lines I need then create the array and go back and start reading stuff into it. However I am using StreamReader and I don't think there is a way to mark a position on the stream and move back to it. How would you do it? Just create a dynamic array and if I need more space then add more?

    zoneset name ERM_Config vsan 1
    zone name DS4000_B_MIRROR vsan 1
    * fcid 0x040100 [pwwn 20:07:00:a0:b8:19:53:20]
    * fcid 0xee0000 [pwwn 20:07:00:a0:b8:18:d1:3f]

    zone name JP550_B vsan 1
    * fcid 0x040000 [pwwn 20:07:00:a0:b8:19:53:1f]
    * fcid 0x040400 [pwwn 10:00:00:00:c9:46:ec:8b]
    David

    The chief cause of failure in this life is giving up what you want most for what you want at the moment.
  • Hoakie
    Established Member
    • Feb 2007
    • 382
    • Iowa
    • Craftsman 21829

    #2
    You can try using a System.Colllections.Arraylist. You can dynamically add/remove to them using the .Add() and .Remove() functions. There are other useful methods such as .Contains() , .IndexOf() and .ToArray()

    However, the one drawback to the ArrayList was that it stored everything as an Object so it will accept ANYTHING, including different data types, AND you had to recast everything on the way out of the list

    Dim myList As New Arraylist
    myList.Add(10)
    myList.Add(20)

    '--Causes Error if using option strict which of course is highly recommeded)
    'Dim x as integer = myList.Item(0)

    '--this works
    dim x as integer = CType(mylist.Item(0), Integer)

    Fortunately with 2005 and Generics, you can create a Type specific list (i.e. only accept strings, integers, myObject) using System.Collections.Generic.List

    Dim myList as New System.Collections.Generic.List(Of integer)

    '--Can't put this in will bomb
    'myList.add ("Hello")

    mylist.Add(10)
    myList.Add(20)

    '--No need to cast
    dim x as integer = myList.Item(0)

    There are a bunch of other collections and generic equivalents you can use depending on your task (stacks, Dictionary, hash, queue)
    Last edited by Hoakie; 09-10-2007, 11:23 PM.
    John
    To invent, you need a good imagination and a pile of junk. ~ Edison

    Comment

    • Alex Franke
      Veteran Member
      • Feb 2007
      • 2641
      • Chapel Hill, NC
      • Ryobi BT3100

      #3
      Like Hoakie says, if you're using 2005, then use a generic System.Collections.Generic.List<> of the type you want to collect. (This is called "type safety" and it can save your behind.) For strings specifically, there's also a built-in class you can use: System.Collections.Specialized.StringCollection will also dynamically grow.

      But if you just want to convert all the lines in a file into strings in an array, forget about the streamreader, and just do this:

      System.String[] lines = System.IO.File.ReadAllLines("D:\\test.txt");

      This basically sucks in the file and stores it as a string array of the appropriate length. No extra overhead. The other benefit of this method is that it lets go of the file immediately -- it opens and closes it right away so you're not hanging on to it while you're working on it.

      Once its in memory just use an int for the line position you want to "bookmark".

      BTW, don't be afraid to read in large files, either.

      If you must use a streamreader, though, I suppose another option would be to use ReadToEnd() and then use Split() to separate it into an array of lines.
      online at http://www.theFrankes.com
      while ( !( succeed = try() ) ) ;
      "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

      Comment

      • Alex Franke
        Veteran Member
        • Feb 2007
        • 2641
        • Chapel Hill, NC
        • Ryobi BT3100

        #4
        I forgot -- that was C#. In VB it would look something like this:

        Dim lines() As System.String = System.IO.File.ReadAllLines("D:\test.txt")
        online at http://www.theFrankes.com
        while ( !( succeed = try() ) ) ;
        "Life is short, Art long, Occasion sudden and dangerous, Experience deceitful, and Judgment difficult." -Hippocrates

        Comment

        • 25
          Established Member
          • Jan 2004
          • 294
          • League City, Tx, USA.
          • BT3100

          #5
          I know this is a VB question but I seriously have to ask, can't you use a better scripting language? I mean perl and python(ug) should be able to handle this with no problem what so ever and are much nicer to use.

          In perl all arrays are dynamic and you can just ask the array it's current length. You could easily do the whole operation in one go.

          To get back to your original question though, could you do something like a linked list? That would allow you to not care about the array length of each line and still parse each line one item at a time. Ok nevermind Hoakie already answered that question.

          Comment

          • crokett
            The Full Monte
            • Jan 2003
            • 10627
            • Mebane, NC, USA.
            • Ryobi BT3000

            #6
            Alex,

            I was using StreamReader/Writer because I am porting another not-really-a-programmer's existing code to VB .Net and when I googled file I/O that is what came up. What you suggested looks better. And I know enough C,C++ etc to know what it was. I can read about half a dozen languages. Just can't write in any of 'em.

            25, I wanted to do Perl but am working with some existing code where a lot of the work has been done. Don't want to totally reinvent the wheel. Plus future I hope to draw a Visio diagram on the fly - this is what the arrays are for. I don't think Perl has an API for that.
            Last edited by crokett; 09-11-2007, 09:32 AM.
            David

            The chief cause of failure in this life is giving up what you want most for what you want at the moment.

            Comment

            Working...