in reply to finding unique items in an array, from a text file
I'm assuming this isn't homework, then. You're over thinking things, for one. Simplify it. There's no need to build an array of lines just to build a hash of lines, for example.
Beyond over thinking the problem, you have some specific issues with your code:
use strict; use warnings; # you left this out my $file = "controls.txt"; open (my $fh, '<', $file) or die "Can't open $file for read: $!"; # Don't use two-arg open unless you know why, and use # lexical filehandles unless you know of a reason not to. my %seen; while ( <$fh> ) { $seen{$_} = 1; # Later assignments to an identical key # overwrite earlier ones, so there's really # no need to check if all you want is uniqueness. } my @unique = keys %seen; print @unique;
This could, for a very simplified version, boil down to this:
perl -le '$seen{$_}++ while <>; print keys %seen;'
Under perl 5.10, this is a decent Perl golf version:
perl -lE'$s{$_}++for<>;say keys%s'
Don't play golf with code you intend to use for serious work or for a school project. I did it here just to show you how much you are complicating the task for yourself. There are actually very few things going on in this code on the level at which Perl is capable of processing it for you. The only steps you need are:
|
|---|