# print-first.pl. See Perl Monks [id://11154123]. use strict; use warnings; my $fname = shift or die "usage: $0 file\n"; open( my $fh, '<', $fname ) or die "error: open '$fname': $!"; my %hash; my $line; while ( defined($line = <$fh>) ) { chomp $line; $line =~ s/^\s+//; # remove leading $line =~ s/\s+$//; # and trailing whitespace next unless length $line; # ignore empty lines next if $line =~ /^#/; # ignore comment lines next unless $line =~ /^(\w+)\s+(\d+)$/; # ignore lines that do not match my $key = $1; my $val = $2; next if exists $hash{$key}; # ignore if already seen ++$hash{$key}; print "$key $val\n"; } close $fh;