Exercise: display unique rows of a file

Given a the following file:

examples/rows_of_file.txt

A 1
B 2
A 2
C 3
B 2
D 4
C 1 

Create another file in which the first character is unique:

A 1
B 2
C 3
D 4

Comments

Sorry, I have overlooked in to the actual question. Added the steps to redirect the o/p to a file

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; ######

my $file = 'file'; my $ofile = 'out'; open my $fh, '<', $file or die "$!\n"; open my $ofh, '>', $ofile or die "$!\n"; my %seen; while (<$fh>) { chomp; my ($k, $v) = (split /\s+/)[0,1]; if ( ! $seen{$k} ) { $seen{$k} = $v; }

}

close($fh);

foreach my $k (sort keys %seen) { print $ofh "$k $seen{$k}\n"; }

-bash-3.2$ ./unique.pl A 1 B 2 C 3 D 4


open ( FH, "Infile.txt" ) || die "error :$!\n";

my @FileARR = ; chomp(@FileARR); close FH;

my %SeenHASH = ();

open (FH,">OutFile.txt" ) || die "error :$!\n"; foreach my $line ( @FileARR ) { my ($ch,$val ) = split( /\s+/, $line); next if ( exists $SeenHASH{$ch} ); $SeenHASH{$ch} = $line; print FH "$line\n"; }

close FH;

---

Please a minimal set of good parctices: use strict, use warnings, and use 3-paramter open. (See articles for each one of these.)