Hello, I'm doing some word counts in Perl and have a problem with the following one. I want to use two subroutines count and show and do a word count on two lines of an array at the same time
I've got two problems with it.
1) when using print at the end of the loop in the count subroutine, I get
1 just
1 trying
1 figure
1 this
1 out
1 cant
2 figure
1 how
but I think, I should get
1 just
1 trying
1 this
1 out
1 cant
2 figure
1 how
in some tries only, one of the two lines is counted when changing the curly brackets.
so, 1) how to get the correct count?
and 2) how to store the result to show it with the show subroutine?
Thanks for your help
# word count perl
my @chains = (
"just trying to figure this out,",
"cant figure how to do it…",
);
my %frequence = count( 3, @chains );
show( %frequence);
sub count {
die "2 arguments needed"
if @_ <2;
my $n = shift @_;
# foreach and split
foreach my $chain (@chains) {
my @array = split(/[^a-zA-Zàâäéèêëîïôöùûüç]+/, $chain);
# grep
@long_chains = grep { length $_ >= $n } @array;
# frequencies
foreach my $word ( @long_chains ) {
$frequence{$word}++;
print "$frequence{$word} $word\n";
}
}
}
sub show {
# hash
foreach my $word ( sort keys %frequence ) {
print "$frequence{$word} $word\n";
}
}