Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7011547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:08:29+00:00 2026-05-27T22:08:29+00:00

i’m looking for a method for reading data from a txt file. The text

  • 0

i’m looking for a method for reading data from a txt file. The text file have this kind of structure (fixed length fields):

0000       AAAAAA     BBBBBB   CCCCCCCC
0000       JJJJJJ     III      RRRRRR
1111       XXXX       YYYYYYYY ZZZZZZZZ
1111       WW         PPPPPPPP ZZZZZZZZ
1111       XXXX       YYYYYYYY ZZZZZZZZ
2222       XXXX       YYYYYYYY ZZZZZZZZ
...

I have to get them in groups by first field, in somekind of list of dictionary lists or something like this. For this particular example the solution would be:

id(list): 0000,1111,2222.....
(content)List: 0000
    field1(list): AAAAAA,JJJJJJ
    field2(list): BBBBBB,III
    field3(list): CCCCCCCC,RRRRRR
(content)List: 1111
    field1(list): XXXX,WW,XXXX
    field2(list): YYYYYYYY,PPPPPPPP,YYYYYYYY 
    field3(list): ZZZZZZZZ,ZZZZZZZZ,ZZZZZZZZ
(content)List: 2222
    field1(list): XXXX...
    field2(list): YYYYYYYY...
    field3(list): ZZZZZZZZ...

Right now i have the the whole txt stored in a list of strings (one per line).

How can i do this in vbnet? Do you think there’s a better approach to this problem?

Thanks and have a happy new year

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:08:30+00:00Added an answer on May 27, 2026 at 10:08 pm

    You could use LINQ to create a Dictionary with the first column as key:

    Dim fileName = "C:\Temp\Test.txt"
    Dim allLines = IO.File.ReadAllLines(fileName)
    
    Dim query = From line In allLines
            Select Columns = Microsoft.VisualBasic.Split(line, vbTab)
            Select ID = If(Columns.Count <> 0, Columns(0), " "), Values = Columns.Skip(1).ToList
            Group By ID Into Group
            Select ID, Group
    
    ' create a Dictionary from the LINQ-Query '
    Dim dict = query.ToDictionary(Function(grp) (grp.ID))
    ' iterate all Dictionary-Entries '
    For Each entry In dict
        Dim key = entry.Key ' f.e 0000 
        For Each grp In entry.Value.Group
            Dim id = grp.ID ' f.e 0000 (can repeat how we see in your example) '
            Dim values As List(Of String) = grp.Values
            ' f.e. (0) "AAAAAA"  (1) "BBBBBB" (2) "CCCCCCCC" '
        Next
    Next
    

    If you are not familiar with LINQ (and you are using .NET 4.0) i would suggest to use a List(Of Tuple(Of String, List(Of String))) since duplicates are allowed(dictionary is no option).

    Dim data = New List(Of Tuple(Of String, List(Of String)))
    For Each line In allLines
        Dim Columns = Microsoft.VisualBasic.Split(line, vbTab)
        Dim ID = If(Columns.Count <> 0, Columns(0), "[empty-line]")
        data.Add(Tuple.Create(ID, Columns.ToList))
    Next
    ' iterate the collection and read values '
    For Each item In data
        Dim ID = item.Item1
        Dim Columns = item.Item2
    Next
    

    http://msdn.microsoft.com/en-us/library/system.tuple.aspx


    Edit:

    If you cannot use Tuples because you are not using .NET 4.0, try following “oldschool”-approach that creates a Dictionary(Of String, List(Of List(Of String))).

    It produces the exact desired result (unlike the other ways, because i’ve misunderstood your requirement a little bit so far):

    Dim allLines = IO.File.ReadAllLines(fileName)
    Dim data As New Dictionary(Of String, List(Of List(Of String)))
    For Each line In allLines
        Dim cols = line.Split(ControlChars.Tab)
        Dim ID As String
        If cols.Length <> 0 AndAlso cols(0).Length <> 0 Then
            ID = cols(0)
            If data.ContainsKey(ID) Then
                Dim columnLists = data(ID)
                For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                    If columnLists.Count >= colIndex Then
                        Dim columnList = columnLists(colIndex - 1)
                        columnList.Add(cols(colIndex))
                    Else
                        Dim newColumnList As New List(Of String)
                        newColumnList.Add(cols(colIndex))
                        columnLists.Add(newColumnList)
                    End If
                Next
            Else
                Dim columnLists = New List(Of List(Of String))
                For colIndex As Int32 = 1 To cols.Length - 1 'skip first(id)-column
                    Dim newColumnList As New List(Of String)
                    newColumnList.Add(cols(colIndex))
                    columnLists.Add(newColumnList)
                Next
                data.Add(ID, columnLists)
            End If
        End If
    Next
    

    How you can read the values:

    Dim idList = data.Keys ' List of all ID-Keys '
    For Each id As String In idList
        Dim content As List(Of List(Of String)) = data(id)
        Dim field1List As List(Of String) = content(0)     ' AAAAAA,JJJJJJ
        Dim field2List As List(Of String) = content(1)     ' BBBBBB,III
        ' ....
    Next
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.