Ive alread read the CSV into the array, Im trying to essentially map the array to a structure without having to assign every value manually. Does that make sense?
Main Topics
Browse All TopicsIm parsing comma seperated values out of a text file and I would like to bascially map it to a structure automatically. There are over 50 fields that need to be matched, so Im trying to find a way to do this without having to code each value. The text file comes in in the same order that the structure is coded so assuming there are no problems in the comma seperated values its pretty much a 1:1 mapping. Is there an easy way to do this or am I going to have to assign all 50 values manually?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Just open the file, store it into a buffer, split it into an array, and then loop through it to assign to your structure.
--------------------------
Dim FF As Integer, Buffer As String, myArray() As String, i As Integer
FF = FreeFile
Open "C:\YourFile.txt" For Binary As #FF
Buffer = Space(LOF(FF))
Get #FF, , Buffer
Close #FF
myArray() = Split(Buffer, ",")
For i = 0 to UBound(myArray)
' Do what you want here with myArray(i)
Next
--------------------------
If you have more than one comma delimited field, just split it twice. once by vbCrLf (or vbCr or vbLf, depending on how your file is made), and then split each field by a comma in a nested loop.
Unfortunately, angel||| is correct; there is no really easy way around this.
However, we can produce a work-around. I'm assuming your structures fields are all named differently, with no real pattern between them. What we can do is analyze your type in a "temporary" program, get all the fields using reflection, copy them to your clipboard, and assign them manually, without actually typing it all out. I'll type the psuedocode out, but you let me know if it's what you're looking for. If so, we'll get to the code.
- Store the data in an array using File.ReadAllText and String method Split to split by the commas (not very memory efficient, but hey, we'll get to that later)
- Step through the fields in your type (using Reflection), and copy a basic structure to your clipboard. You can then paste this code directly into your original class, and viola, foolproof code done automatically.
Let me know,
Nate
Wow theres been a couple posts since I started that comment. The whole process is pretty simple. I have a text file with lines of comma seperated values. However one line may not have the same amount of columns as the next. Unfortunately these are standard formats used in banking and are sent to us by banks to record transactions so I can not modify the file. I simpleyhave to parse it as is and deal with the ouput. What I am doing is writing a class to verify the file before they go through the data transformation application and dropped into the databases.That application doesnt handle malformatted files well. Anyhow thats just a little background on the issue.
So I have a document with the definitions of all the colums, and different types of lines in the file (distrobution, trade, check/eft payment, etc). Im starting with the distrobution entry, which in this case has 41 columns and is identified by the first column with a value of "D". So I created a structure that represents the distrobution entry that contains all of the columns from the definition. So I read the line in the text file, and using a regex expression, parse out the the values into an array of strings. From that point I wanted to read those values into the structure in the exact order that they appear in the structure. The problem itself is not difficult, I was simply trying to avoid having to change a bunch of code if the columns change. I wanted to write it such that I would only need to change the structure and recompile to support new fields. That and some of the other entries have a lot more than 41 colums and I wanted to avoid having to manually code assignments for every single column.
So will the ordinal of the column always be the same in each line? If so, you can just use an array in your class, whip through the lines of the file, and split the lines by the delimiter (probably a comma, since it's a CSV). See the code snippet for an example, and let me know if this is what you're thinking.
You can have any dictionary object (hash table, for example) and an array of field names
dim arrFields() as string={"field1","field2",
dim htStructure as New HashTable
'read one line and split it into arrOneLine array
for i as integer=0 to arrOneLine.length-1
htStructure.Add(arrFields(
Next
When columns order/count change-just change arrfFelds array
See thats the thing, each line may change. For example a line of distrobution type "D" will always be immediately followed by a EFT transfer "E" or a check line "C" which have only 8 and 12 columns respectivly. I cant simply parse the entire file or parse every line the same for that matter, I have to parse line by line and run tests to validate each line. So basically parse out the line into an array of strings, and take the first column of each line to determine the type.
So seeing as how there appears to be no way to do that without having to manually assign values, ive decided to just abandon trying to parse it into a class at all. Ill have to find a different way to do this.
So what Im thinking at this point is to just read the "rules" for each line type into a class and just verify each incoming line against the rules class based on its line identifier. This way at least I dont have to store each line, and to add new types I just add properties to the rules class. I can take that a step further and just store the rules in an xml file so I dont have to modify the code at all to add new files. I lose some possible functionality in terms of what I can do with each line but Im not sure I will need to be building on that too much anyhow.
Angel, good suggestion would probably make parsing subsequent lines much easier. I hate these obscure file formats that they send us. Its like these things were designed by accountants and not programmers. Most of them make no sense and are very difficult to parse, at least this one is CSV.
Though ultimately what I was looking for is just some method for enumerating properties of a class. I think the reflection technique is probably the only way I could really do that, though it might take some "jerry rigging."
Hi
In your case you can add one more hashtable:
Dim htFields As New HashTable
dim htStructures As New HashTable
htFields.Add("A",New String(){"Field1","Field2"
htFields.Add("B",New String(){"Field1","Field2"
htFields.Add("C",New String(){"Field1","Field2"
'............
'Note - you can populate above ht from *.ini text or xml file without recompiling project if rules changes.
dim sKey as string = arrOneLine(0)
arrFields=htFields(sKey)
for i as integer=0 to arrOneLine.length-1
htStructure.Add(arrFields(
Next
You can have an array (or new hash) of htStructures for all liines
Business Accounts
Answer for Membership
by: angelIIIPosted on 2009-06-23 at 11:37:26ID: 24694629
to get a comma-separated string into an array is simple: the string class has a split method: en-us/libr ary/ system .string.sp lit.aspx
http://msdn.microsoft.com/
as from there, you can loop on the resulting array...