If you are looking to put them in order, then you are better off using a hash of hashes (with an array :) ). For instance:
#!/usr/bin/perl
use warnings;
use strict;
my $usage = "merge_bed.pl <input1> <input2> <output>";
my $input_1 = shift or die $usage;
my $input_2 = shift or die $usage;
my $output = shift or die $usage;
open my $in1, "<", "$input_1" or die "Cannot open $input_1: $!\n";
open my $in2, "<", "$input_2" or die "Cannot open $input_2: $!\n";
open my $out, ">", "$output" or die "Cannot open $output: $!\n";
my %bed_files = ();
while ( <$in1> ) {
chomp;
my ($chrom, $start, $end, undef, undef, $strand) = split "\t";
$bed_files{$chrom}{$start}[0] = $end; # you can save multiple valu
+es as an array, so both the end and strand and anything else you want
$bed_files{$chrom}{$start}[1] = $strand;
}
while ( <$in2> ) {
chomp;
my ($chrom, $start, $end, undef, undef, $strand) = split "\t";
$bed_files{$chrom}{$start}[0] = $end;
$bed_files{$chrom}{$start}[1] = $strand;
}
for my $chrom (sort keys %bed_files) {
for my $start (sort {$a <=> $b} keys %{$bed_files[$chrom}}) {
# print out the results sorted by chromosome (or scaffold) and
+ start site
print $out "$chrom\t$start\t$bed_files{$chrom}{$start}[0]\t$be
+d_files{$chrom}{$start}[1]\n";
}
}
close $in1;
close $in2;
close $out;
exit;
This assumes that your lists don't have a) some of the same start sites and b) that there are not overlaps. If you want to find overlaps, then you can do the same thing, but have two separate data structures; you can find the overlaps, and then output the unique regions and one copy of the overlapping regions. There are ways to do this using an index (so it's faster and you only make one pass instead of looping though the entire bed file multiple times).
You could create function for code generating the main data structure, but I left it as is just so it's easier to read and see what I'm doing. Have fun!
EDIT: Fixed a couple of typos!