Is there a way to merging two TLists?

Hi people,

I am trying to select multiple txt files from multiple directories, and then have root open the selected files one at a time.

I have used the tgfileinfo class to bring up the file selection dialog to select multiple files which are then returned in a tlist. And then, if the user still needs to select more files from another directory, the dialog is brought up again and so on. However, this gives me mutliples tlist to work with.

I was wondering if there was a way of merging the tlist properly. I tried the Merge() method but it didn’t seem to work. :frowning:

Cheers,
Atyab

Any help anyone? :frowning:

Hi Atyab,

[quote=“azou191”]I tried the Merge() method but it didn’t seem to work.[/quote]What do you mean by “it didn’t seem to work”? How did you try? What was the result? Could you provide some concrete example (e.g. running macro)? Did you look at the TList::Merge() documentation?

Cheers, Bertrand.

What do you expect from this operation? I mean, possible semantics :

  1. Both lists are intact after this operation (like they are immutable) and you have a new list
  2. The first (the “left”) is modified (contains the result of a merge) and the second (the “right”) is now empty

The first solution does not require any special function, you just iterate on both lists and add object into the new one. The second case - is not supported directly (such a function is called splice for std::list), but you can easily implement it … well, inheriting TList (after all, data-members are protected and you can access them, simply “cutting” the structure from one list an pasting it into another), or even easier (though quite non-optimal) like the first one.

The TSeqCollection::Merge you are trying to use indeed is more sophisticated than this since it cares about contained objects’ structure.

EDIT: I forgot completely our collections are not copyable and the fact that you can not simply take an object from one collection and put it into another. So only a “splice” can be an option, but I have to think how to do a trick :slight_smile:

Hi Bertrand and tpochep,

@ Bertrand
I attached a highly watered-down version of my macro but which still retains what I’m trying to do. It’s just for selecting two files in succession and then opening them or something. I did have a look at the TList::Merge() documentation and if I understood this line correctly,

[quote]If some objects inside the collection are instances of a class that do not
have a Merge function (like TObjString), rather than merging, a copy of each
instance (via a call to Clone) is appended to the output.[/quote]

,I thought what I did should work.

@tpochep
I tried iterate on the second list and adding to the first one but I cannot add TObjString to a TList. And I’m not sure I understand your second case…

Cheers,
Atyab
test_merge.C (1.97 KB)

OK, then you can simply do the merging this way:

TIter next2(file_info_2.fFileNamesList); TObjString *file2; while ((file2=(TObjString *)next2())) { if (!file_info_.fFileNamesList->FindObject(file2->GetName())) file_info_.fFileNamesList->Add(new TObjString(file2->GetName())); }
Cheers, Bertrand.

It works! ^^

Thank you so much for your reply! :slight_smile:

Cheers,
Atyab