#! perl -w use strict; my ($file, $col, $file2, $col2) = @ARGV; my (%unique, %unique2); my @column; my ($x, $header); my @fields; # I put something like this in all my scripts so I don't need to remember argument order if ($file eq "?"){ die "usage: compcolumns.pl file1 column1 file2 column2\n"; } ## whenever you open a file, do yourself a favor and tell yourself if it doesn't open open FIL, $file or die "Could not open $file: $!\n"; $x = 0; %unique=(); while (){ chomp; ##The split here, a regular expression, is what you would change (at least if your data is based on space delimiting instead of tab-delimiting). @fields = split /\t/; if (defined ($fields[$col]) ){ $header = $fields[$col] if $x == 0; $unique{$fields[$col]}++; } else{ $unique{"BLANK"}++; $header = "BLANK"; } $x++; } open FIL, $file2 or die "Could not open $file2: $!\n"; $x=0; while (){ chomp; @fields = split /\t/; if (defined ($fields[$col2]) ){ $header = $fields[$col2] if $x == 0; $unique2{$fields[$col2]}++; } else{ $unique2{"BLANK"}++; $header = "BLANK"; } $x++; } ## The output routine. Note that if something's in both files, it will be printed twice. foreach (sort keys %unique) { if (!exists $unique2{$_}){ print "$_ in $file but not $file2\n"; } else { print "$_ in both files\n"; } } foreach (sort keys %unique2) { if (!exists $unique{$_}){ print "$_ in $file2 but not $file\n"; } else { print "$_ in both files\n"; } }