title: How to parse the same element of multiple files at the same time in Perl
body: <p>I want to merge several xml files in perl. Each file is composed of a lot of elements; I need to merge data with the same element from these files. e.g. </p>
  
  <p>file1 has elements {e1, e2, e4}</p>
  
  <p>file2 has elements {e1, e3, e4}</p>
  
  <p>file3 has elements {e2, e4, e5}</p>
  
  <p>so I need to merge e1 of file1 with e1 of file2, merge e2 of file1 and e2 of file3, etc. the merged result will be saved in another file.</p>
  
  <p>Since the size of these files are big, it is not good to merge data file by file, i.e. parse the whole file1, then parse the whole file2 and merge it with file1. etc. because that will consume a lot of memory.</p>
  
  <p>So I plan to merge data element by element. i.e.  parse e1 of all files, release memory, then parse element2 of all file, release memory, etc.</p>
  
  <p>Currently I use xml:parser: sax parser to parse and handle the files.</p>
  
  <p>My question is how to implement the merge element by element. I do not know how these files can be controlled to process the same element. Using conditional signal? fork() Or sth. else? Can anybody gives me an example here because I am not familiar with either way. Thx.</p>
  
  <p>here is the example how the data is merged:
  file1:</p>
  
  <p><code>&lt;class name="math"</code>></p>
  
  <p><code>&lt;string</code>>luke1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>luke2<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p><code>&lt;class name="music"</code>></p>
  
  <p><code>&lt;string</code>>mary1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>mary2<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p>file2:</p>
  
  <p><code>&lt;class name="math"</code>></p>
  
  <p><code>&lt;string</code>>luke1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>luke3<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p><code>&lt;class name="music"</code>></p>
  
  <p><code>&lt;string</code>>mary1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>mary3<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p><code>&lt;class name="english"</code>></p>
  
  <p><code>&lt;string</code>>tom1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>tom2<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p>should be merged to another file as:</p>
  
  <p><code>&lt;class name="math"</code>></p>
  
  <p><code>&lt;string</code>>luke1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>luke2<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>luke3<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p><code>&lt;class name="music"</code>></p>
  
  <p><code>&lt;string</code>>mary1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>mary2<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>mary3<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p><code>&lt;class name="english"</code>></p>
  
  <p><code>&lt;string</code>>tom1<code>&lt;/string</code>></p>
  
  <p><code>&lt;string</code>>tom2<code>&lt;/string</code>></p>
  
  <p><code>&lt;/class name</code>></p>
  
  <p>Note I want to merge element math of all files, then merge element music of all files and then merge element english of all files.</p>