my %hValuesInColumn2; # use while, not foreach while (my $line = ) { #get rid of trailing new line chomp $line; # strip remaining leading and trailing whitespace # (if that is an issue) $line =~ s/^\s+//; #leading $line =~ s/\s+$//; #trailing # record field value $hValuesInColumn2->{$line} = 1; } # extracting the keys may not even be necessary # see first part of this answer. my @unique_f = keys %hValuesInColumn2;