Merging of files with different headers to make combined headers file

Hi ,
I have a typical situation. I have 4 files and with different headers (number of headers is varible ).
I need to make such a merged file which will have headers combined from all files (comman coluns should appear once only).

For example -
File 1
H1|H2|H3|H4
11|12|13|14
21|22|23|23

File 2

H3|H5|H6
31|32|33
41|42|43

Merged file of these two will be -

H1|H2|H3|H4|H5|H6
11|12|13|14|NA||NA
21|22|23|23|NA|NA
NA|NA|31|NA|32|33
NA|NA|41|NA|42|43

Please help me for the same.

if already know the maximum and sequence of headers, should below easy perl script help you some.

If not, may need extra efforts to make out the header hash first and then use below perl script.

my %headers=(H1=>1,H2=>2,H3=>3,H4=>4,H5=>5,H6=>6);
my @header_arr = sort {$headers{$a}<=>$headers{$b}} keys %headers;
print join "|", @header_arr;
print "\n";
my %missed;
my $max=$#header_arr;
while(<DATA>){
	chomp;
	my @tmp=split("[|]",$_);
	my %hash=%headers;
	if($.==1){
		map {delete $hash{$_}} @tmp;
		map {$tt{$_}=1} values %hash;
		next;
	}
	else{
		for(my $i=1;$i<=$max+1;$i++){
			if(exists $tt{$i}){
				print "NA|";
			}
			else{
				my $tmp=shift @tmp;
				print $tmp,"|";
			}
		}
	}
	print "\n";
}
__DATA__
H1|H3|H6
12|31|33
23|41|43