How do I count the total number of unique strings from a file using Perl? Any help is appreciated..
Assuming by unique you mean 100% unique, and things like case or white space are included in the uniqueness:
use strict;
use warnings;
my %unique;
open my $FH, '/path/to/your/file' or die "$!";
while(<$FH>) {
chomp;#<-- remove this line if the record seperator should be incuded
$unique{$_};
}
close $FH;
foreach my $line (keys %unique) {
print "$line = $unique{$line}\n";
}
Next time some effort on your part to first try and solve your programming requirements would be nice to see.
Definitely. Thanks a lot.
I'm not sure what happened in the code I posted, but this line:
$unique{$_};
should be:
$unique{$_}++;
So it displays the count of each unique line
yes, I got an error message "Useless use of hash element in void context ....filename...
Thanx.
---------- Post updated at 02:40 PM ---------- Previous update was at 02:20 PM ----------
I've made changes to the code to handle utf8 as :--
use encoding 'utf8';
my %unique;
# my $unique;
my $line;
open my $FH, "<:encoding(utf8)", "$ARGV[0]" or die "Can't open file $ARGV[0]: $!";
while(<$FH>) {
chomp;
$unique{$_}++;
}
close $FH;
foreach my $line (keys %unique) {
print "$line = $unique{$line}\n";
}
However, I am getting the message....
2 files to edit.
I don't know this.
is the message coming from die or when you print the hash or something else? That sure is not any perl error or warning I have ever seen.
Thanks a lot.
I worked.