#!/usr/local/bin/perl
# combines new experiments and old experiments into a new file,
# sorted in the right order with old experiments that were redone removed,
# so as not to confuse the experiment parser.
# (OLD) strips out the experiments from the comma-separated list
# (OLD) on stdin, so that concatenating new replacements (for eliminating
# (OLD) high CVs) will not confuse the experiment parser.

$original = $ARGV[0];
$redo = $ARGV[1];

if (not defined($original) || not defined($redo)) {
	die "Usage: strip-experiments exp-orig exp-redo > exp-new";
}

$origExps = parseExps($original);
$redoExps = parseExps($redo);

foreach $expKey (sort {$a<=>$b} keys(%$origExps)) {
	if (exists($redoExps->{$expKey})) {
		$getFrom = $redoExps;
	} else {
		$getFrom = $origExps;
	}
	print join('', @{$getFrom->{$expKey}});
}

sub parseExps {
	my $filename = shift;
	my $exps = {};

	open (EXPS, "$filename") or die("opening $filename: $!");
	while (<EXPS>) {
		my $expnum = -1;
		if (m/^EXP (\d+)/) {
			$expnum = $1;
		} elsif (m/^(\d+) /) {
			$expnum = $1;
		} else {
			next;
		}
		$i = int($expnum);
		push @{$exps->{$i}}, $_;
	}
	close EXPS;
	return $exps;
}
