Reputation:
When I run this script, it only outputs the first line of my text file.
It is supposed to take a list of email addresses and output the ten most occurring domains.
For example, [email protected] => 4 [email protected] => 3
#!D:\Perl\bin\perl
#Read file
open F, "<Emails.txt" or die $!;
while (<F>) {
print $_;
{
close F;
perl ne
$s{$_}++
}{
print map "$s{$_}: $_",
(sort {$s{$b} <=> $s{$a}} keys %s)[0..9] }}
Upvotes: 0
Views: 1859
Reputation: 7912
You appear to be trying to use @mpapec's answer to: Script for a list of data on STDIN returning most common values
You should pass the contents of Emails.txt
into that command:
perl -ne '
$s{$_}++
}{
print map "$s{$_}: $_",
(sort {$s{$b} <=> $s{$a}} keys %s)[0..9]
' Emails.txt
However, this answer doesn't cater for extracting the domain. To do that you'd need to replace the first line with:
$s{(split /@/)[1]}++
Upvotes: 0
Reputation: 2783
what you are trying to do is like this which will close filehandle at the end of 1st while loop hence you are getting first line only . hence you need to close it outside filehandle
open (FH ,'<',"text.txt") or die "can't open";
while(<FH>)
{
print $_;
close FH;
}
try this:
open (FH ,'<',"text.txt") or die "can't open";
while(<FH>)
{
print $_;
}
close FH;
Upvotes: 4
Reputation: 272257
Doesn't this:
while (<F>) {
print $_;
{
close F;
close your filehandle within your while() loop i.e. after your first line ? I suspect you need something like:
while(<F>) {
# do something
}
close(F);
Upvotes: 5