Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET General > using Split, LineInput, and an Array


Reply
 
Thread Tools Display Modes
  #1  
Old 07-13-2005, 01:04 AM
Sigma Sigma is offline
Centurion
 
Join Date: May 2005
Location: Alberta, Canada
Posts: 110
Default using Split, LineInput, and an Array


I've searched these forums but gave up trying to find something similar to the problem I'm having, so any help would really be appreciated. (using vb.net btw)

I have a text file with hundreds of lines like so:

AT.V, AmericanTitans
BT.V, BulvidareTitans
etc

Where they are seperated by a comma. I want to send these values to seperate arrays and the best I can do is the following:

Code:
 
        Dim Fnum As Integer = FreeFile()
        FileOpen(Fnum, "C:\StockTracker\SymbolList.txt", OpenMode.Input, OpenAccess.Read)

        Do While Not EOF(Fnum)
            ASymbol = Split(LineInput(Fnum), ",", -1)
        Loop

        For i As Integer = 0 To 10
            MsgBox(ASymbol(i))   'just going to 10 as a test, it should go up to ASymbol.getupperbound(0) I believe?
        Next
        FileClose(Fnum)
The problem with this, is I just get two values in my array, the values in the last two lines of the text document. Is there anyway to get all of the information to the left of the comma in one array, and all of the info on the right of the comma into a seperate array?

Thanks alot for any suggestions.
Reply With Quote
  #2  
Old 07-13-2005, 01:26 AM
TeraBlight's Avatar
TeraBlight TeraBlight is offline
Captain Convoluted

* Expert *
 
Join Date: Jun 2005
Posts: 1,918
Default

The problem is that you are overwriting ASymbol for every line.
After the first line, it will contain {AT.V, AmericanTitans}, after the second line, {BT.V, BulvidareTitans}, etc.
What you want to do is either
a) count the lines in the file first, then use a for loop instead of the do loop, and use three arrays to get hold of the segments like this:
Code:
ATemp = Split(LineInput(Fnum), ",", -1) ASymbol(i) = ATemp(1) AName(i) = ATemp(2)
or

b) read in the whole file at once, then split by linebreak, then transfer from ATemp to the separate arrays (in a for loop, again):
Code:
ASymbol(i) = Split(ATemp(i), ",", -1)(1) AName(i) = Split(ATemp(i), ",", -1)(2)
(I hope this works in .net, you're in the wrong forum )
__________________
"To learn without thinking is to labour in vain" - Confucius
Reply With Quote
  #3  
Old 07-13-2005, 01:38 AM
Sigma Sigma is offline
Centurion
 
Join Date: May 2005
Location: Alberta, Canada
Posts: 110
Default

Thanks, guess I didn't realize the limitations of Split. Since I'm in the wrong forum, I'll ask a VB question then. In order to take the whole file in at once, do I have to stream it in? And lets say I do get it as one long string, what would be the best way to go about dividing up the values between all the commas? This would be for a different file layout that I have, where information is not broken up by lines, just one long massive line of deliminated information.

Thanks again, I like the first solution you provided me with.
Reply With Quote
  #4  
Old 07-13-2005, 01:53 AM
TeraBlight's Avatar
TeraBlight TeraBlight is offline
Captain Convoluted

* Expert *
 
Join Date: Jun 2005
Posts: 1,918
Default

Have a look at the File I/O tutorial, especially this post, it explains how to read in files in many different ways:
http://www.xtremevbtalk.com/showthread.php?p=661322
__________________
"To learn without thinking is to labour in vain" - Confucius
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->