Coding a Merge Function


I need help for coding a merge function.

I wrote a prototype that works fine using that merge schema: I took a test file to load and another text file who have missing entries and it works fine. But once it came the time to enlarge that method to accept unknow merge text file, the troubles starts.

Below is my Merge window:

On the left: the Heading for the target ListBox,
On the right, the Headinf-g from the (text) file to Merge.

As you can see, I can have a different set of columns and, on screen (that window), I can set the Right Headings to the Left Headings, but once I am there, I am totally unable (since weeks) to go ahead.

I need a direction to how I can take the merge text line(s) into the right colum. Doing so with know contents is easy, doing it from dark (a multi purpose load following the user schema) is where I am lost.

Actually, I have something like:

// If the loaded line have to be added [code OK]
ListBoxTarget.AddRow “”
LocRow = ListBoxTarget.ListIndex

// Then I have a set of:
ListBoxTarget.Cell(LoopIdx,LocRow) = NthField(Loaded_Line,Chr(9),CellNbr) // What CellNbr ???

end of loop[/code]

Thank you for a tip that will help me to make the code working as in the window screen shot case above (or any other case).

PS: forgot the date area in top-right: I added it, but I do not think it will stay there (as I use everywhere ISO 8601 dates).
Also, the value on the right listbox is a value stored in the RowTag corresponding to each Row entry.

  1. Decouple your data from your interface. Handle the data first then the interfaces can show the result.
  2. What is your data structure? Array, dictionary? A dictionary handled duplicated automatically. For an array you can do a duplicate check when adding. Or add everything into the array, sort and fish out duplicates in a last step.

Thank you Beatrix for your answer.

the ui is here to let the user set where a “Column” from the merged text file will go.

The target (place where the merged text will be added) is a Listbox. I took its heading and place the Column names in the List on the left.

If I take a text file, remove some lines from it, paste them (and put the Heading line at the top of the document) in a new document saved with “Data File to be Merged.txt", my code works like a charm.

What I want is to be able to merge a text file with “different structure”, but have some similar columns.

I added the window screen shot on purpose, so the reader may understand (have a clue about) what I wanted to do.

Especially the two empty Rows (list on the right): there is no data to add in the original data (headers in the left Listbox).

Even in French, I think I would have troubles to explain :frowning: Sorry.

Emile, you don’t think like a developer.

What is your data structure? Don’t think in interfaces. If you got a list of columns then you got 2 arrays. How do you merge 2 arrays? Like I described above. After you got your merged arrays then get the data into your columns.


I do not use arrays.

I load the “master” (csv) data, then I load the text to be merged and “on the fly” I add the missing lines in the target Listbox ( l’ancienne - like I would do with my Apple //c).

I have to read your answers at a cooler time and try to understand what you mean (understand how to do that and how it works).


With “Missing entries”, you mean missing columns? Those are the easiest part. The headache is with other rules.

Instead of trying to figure out how to do it, define first what you want to do.

Assume File A has 100 records and File B 1000.

Do you want to:
A - add B to A resulting in 1100 records?
B - merge extra columns from B with existing data from A resulting in 100 records?
C - merge extra columns from B with existing data from A AND add the rest as well resulting in between 1000 and 1100 records
D - ?

With B and C you need to define things like:

  • key fields in both files so you know what/how to map.
  • what file data has the priority
  • what to do with duplicates inside the same file

And like Beatrix said, Listboxes are for the presentation. Handle it first within your code. Then when things are processed/merged, display the results in your Listboxes.

Have a look at these 2 books if you want to learn about the principles:

These are old, but very good.

Now I fully understand if you don’t want to learn. You want to solve your issue. But your questions mostly are of a generic nature that you can easily solve yourself IF you understand for instance here what I mean with solving the data problem first.

The way I’ve done with two KNOW files is to add a row when it does not exists, and if the row exists, check if the cell is populated (do nothing) or empty (paste the corresponding data).

I check the column (0), named Matrix # as defined in the PopupMenu (top-middle of the window) and if I already have that matrix #, I skip the line, else I create a Row. That Matrix # is unique, so no trouble.

Of course, I can forgot something.

I am an autodidact, but I started in 1980 in the computer industry with computers (not micro-computer), then I get a micro-computer (Apple) and started to learn programming with AppleSoft BASIC….

Hi Marco:

thanks for your answer.

Missing Rows and missing Columns.

Your presentation tells mostly what I want to do.

No offense taken, but the Language Reference does not says that explicitely like in: Listbox are for display purposes exclusively. Store your data elsewhere, modify them elsewhere and once done, display the new data into a Listbox".

This change (greatly ?) the problem. Since REALbasic 1.0, I used Listbox as storage… (excepted on one occasion where I store data in a SQLIte DB and use the Listbox as adviced here: it was more natural / faster to achieve that way).

I will re-read your advices later today, thank you.

After drinking a cup of coffee, I recall from where the above window design comes from: a data base text import window (AppleWorks ? Works ? I forgot).

Also, after a simple Google quest, I notices that people use the word Merge where I use Append, etc.

Now I understand a bit better parts of your answers.

What I wanted was to automatize what I’ve done manually with drag and drop from a List (Listbox) or a text document (OS X’s TextEdit).

Say a list of comic strips stories (in the shown example) with more data that are actually stored in a different file (or better: the inverse way: less data to move). WIth less than 100 entries, a manual merge can be done (I’ve done more than that), but with around 20,000 entries… (generated by my own software): I’d better let the computer do what he is best at !

I’ve added a couple of buttons to load File A into an Array declared as Property TextFileA() As String.
In the second button, I load line after line data from File B, but I got an error:

This array have fewer dimensions than you have provided.

at TextFileA(Val1,Val2) in a comparison If line and in an attribution (filling that “cell”) line.

If I delcare TextFileA(1,1) that error disappears, but a different one (in load File A contents into the array):

TextArray.Append One_Line

Where am I wrong and how to get it right ?

BTW: the earlier reported problem still exists. This is definitively not related to a Listbox.

Any clue ?