Text strings in files.

Hi all

I have two files X.txt and Y.txt. The file format of X.txt is :

madras is also the fountainhead of the theosophical movement which spread worldwide .

and second file Y.txt is of the format:

madra|s|nsubj is|cop also|advmod the|det fountainhead|empty of|prep the|det theosophical|amod movement|prep_of which|nsubj spread|rcmod worldwide|dobj .|

In Y.txt file , there are some texts units separated by two "|" (i.e.,madra|s|nsubj ) and some texts are separated by one "|" (i.e., also|advmod).

Considering the two files , the expected output is :

madras|madra|s|nsubj is|is|empty|cop also|also|empty|advmod the|the|empty|det fountainhead|fountainhead|empty|empty of|of|empty|prep the|the|empty|det theosophical|theosophical|empty|amod movement|movement|empty|prep_of which|which|empty|nsubj spread|spread|empty|rcmod worldwide|worldwide|empty|dobj .|

In the final output file, all texts units separated by three "|" (i.e., madras|madra|s|nsubj).
To obtain the expected output file, for each texts unit separated by two "|" in Y.txt file, the first corresponding words is copied from X.txt and separated by "|" with the word from Y.txt file (i.e., madras|madra|s|nsubj) from the corresponding location of the corresponding sentence. And for each texts units separated by one "|" in Y.txt file, the corresponding word with "|empty" string is inserted in between the string separated by "|" (i.e., is|is|empty|cop)

I want to write a Perl script on this.
Thanks in advance.

Can you tell us what you have done?

I have given some answers on your another post, with similar request, maybe you can refer it first.

I prefer a perl script for this if possible.

Help Please.