<commit_before>use File::Basename;

my $usage  = "perl " . basename($0) . " file1 file2...\n";
$usage .= qq{
Takes any number of input lists of the same length and creates a tab-delimited
file with each list as a column.
};

use File::Basename;
use Getopt::Long;

my @files = ();
my $has_names = 0;
my $out_file = 0;

GetOptions ('files|input=s{2,}' => \@files,
            'names!' => \$has_names,
            'outputfile:s' => \$out_file) or die "options misspecified";

my $result = combine_files (\@files, $has_names, $out_file);
print $result;

sub combine_files {
    my $fileptr = shift;
    my $has_names = shift;
    my $out_file = shift;

    my @files = @$fileptr;

    if (@files < 2) { die $usage; }

    my @inputs;
    for (my $i=0; $i<@files; $i++) {
        open FH, "<", @files[$i] or die $usage;
        my @data = <FH>;
        close FH;
        push @inputs, \@data;
    }

    my $num_entries = scalar @{@inputs[0]};
    for (my $i = 0; $i < @files; $i++) {
        if (scalar @{@inputs[$i]} != $num_entries) {
            die "Error: files have different numbers of inputs." . scalar @{@inputs[$i]};
        }
    }
    my $result = "";

    for (my $j = 0; $j < $num_entries; $j++) {
        if ($has_names) {
            my $entry = @{@inputs[0]}[$j];
            $entry =~ /(.+?)\t(.*)/;
            $result .= "$1\t";
        }
        for (my $i = 0; $i < @files; $i++) {
            my $entry = @{@inputs[$i]}[$j];
            $entry =~ s/\n//g;
            if ($has_names) {
                $entry =~ /(.+?)\t(.*)/;
                $entry = $2;
            }
            $result .= $entry . "\t";
        }
        $result .= "\n";
    }

    if ($out_file) {
        open FH, ">", $out_file;
        print FH $result;
        return "";
    } else {
        return $result;
    }
}
__END__

=head1 NAME

combine_files

=head1 SYNOPSIS

combine_files [options]
<commit_msg>added option for "has header"<commit_after>use File::Basename;

my $usage  = "perl " . basename($0) . " file1 file2...\n";
$usage .= qq{
Takes any number of input lists of the same length and creates a tab-delimited
file with each list as a column.
};

use File::Basename;
use Getopt::Long;

my @files = ();
my $has_names = 0;
my $has_header = 0;
my $out_file = 0;

GetOptions ('files|input=s{2,}' => \@files,
            'names!' => \$has_names,
            'header!' => \$has_header,
            'outputfile:s' => \$out_file) or die "options misspecified";

my $result = combine_files (\@files, $has_names, $has_header, $out_file);
print $result;

sub combine_files {
    my $fileptr = shift;
    my $has_names = shift;
    my $has_header = shift;
    my $out_file = shift;

    my @files = @$fileptr;

    if (@files < 2) { die $usage; }

    my @inputs;
    for (my $i=0; $i<@files; $i++) {
        open FH, "<", @files[$i] or die $usage;
        my @data = <FH>;
        close FH;
        push @inputs, \@data;
    }

    my $result = "";
    my @labels = ();
    my $num_entries = scalar @{@inputs[0]};
    for (my $i = 0; $i < @files; $i++) {
        if (scalar @{@inputs[$i]} != $num_entries) {
            die "Error: files have different numbers of inputs." . scalar @{@inputs[$i]};
        }
        if ($has_header) {
            my @heads = split /\t/, (@{@inputs[$i]}[0]);
            foreach $head (@heads) {
                $head = @files[$i] . "|" . $head;
            }
            @{@inputs[$i]}[0] = join ("\t", @heads);
        }
    }


#     if ($has_header) {
#         my $header = join ("\t",@files);
#         if ($has_names) {
#             $header = "\t$header";
#         }
#         $result .= "$header\n";
#     }

    for (my $j = 0; $j < $num_entries; $j++) {
        if ($has_names) {
            my $entry = @{@inputs[0]}[$j];
            $entry =~ /(.+?)\t(.*)/;
            $result .= "$1\t";
        }
        for (my $i = 0; $i < @files; $i++) {
            my $entry = @{@inputs[$i]}[$j];
            $entry =~ s/\n//g;
            if ($has_names) {
                $entry =~ /(.+?)\t(.*)/;
                $entry = $2;
            }
            $result .= $entry . "\t";
        }
        if ($has_header && $has_names && ($j==0)) {
            #clean up the header row
            $result =~ s/^.*?\|//;
        }
        $result .= "\n";
    }

    if ($out_file) {
        open FH, ">", $out_file;
        print FH $result;
        return "";
    } else {
        return $result;
    }
}
__END__

=head1 NAME

combine_files

=head1 SYNOPSIS

combine_files [options]

{'fraction_non_alphanumeric': 0.1958351794417368, 'fraction_numerical': 0.008418254319893664, 'mean_word_length': 1.783600493218249, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7343262', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1618, 'n_words': 431}