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
You could use LINQ to create a Dictionary with the first column as key:
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).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):
How you can read the values: