!/usr/bin/perl use strict; use warnings; my $col_file = shift; my $data_file = shift; open my $columns, '<', $col_file or die "Could not open $col_file - $!\n"; open my $file, '<', $col_file or die "Could not open $data_file - $!\n"); my @columns = split /\s+/, <$columns>; chomp(@columns); # use a hash for quick lookup of columns to skip # here I use a hash slice to assign undef values to the keys stored in @columns my %skip; @skip{@columns} = (); while( my $line = <$file>) { chomp $line; my @raw_data = split /\s+/, $line; # Map and grep are a bit tough for newbies but they are important, powerful tools that should be mastered. # Read from right to left: # Take the list of indexes in @raw_data # (grep) keep only those that do not exist as keys to %skip # (map) and look up the the item at that index in @raw_data # and finally print the resulting list. print join ' ', map $raw_data[$_], grep !exists $skip{$_}, 0..$#raw_data; print "\n"; }