Please help.

I have two files (file1 and file2). I would like to extract the columns from file2 that have their IDs listed in file1. These are big files, with thousands of columns and lines.

file1

Id123B

Id124A

Id125A

file2

Code  sex  id123B  id127  id125A

desired output file:

id123B  id125A

Code i have tried:
        
         #!/usr/bin/perl
         use warnings;
         use strict;
         use feature qw{ say };

        open my $COLUMNS, '<', shift or die $!;
        chomp( my @columns = <$COLUMNS> );

        open my $DATA, '<', shift or die $!;
        my @header = split /\t/, <$DATA>;
        my %column_index;
        @column_index{ @header } = 0 .. $#header;

        @columns = grep exists $column_index{$_}, @columns;

        while (<$DATA>) {
        chomp( my @cells = split /\t/ );
        say join "\t", @cells[ @column_index{ @columns } ];
        }


Your code should work fine, but you are not including the headers in the output. Just add `say join "\t", @columns;` before the while-loop.

BTW, `split /\t/` is equivalent to `split`.  :)
