Hello Perl community,
I am trying to get a simple program working that will count the words in a
text file and put them in a hash with the key being the word and the value
being its frequency in the text file. Here is my code:
---
#!/usr/bin/perl
use strict;
use warnings;
{
open IN, "<file.txt" or die "cannot open file: $!";
my %word_count;
my @input_file = <IN>;
foreach (@input_file){
my @x = split;
foreach (@x){
$word_count{$_}++;
}
}
foreach (%word_count){
print "$word_count{$_}\n\n";
}
}
---
The problem is on the 3rd to last line I get this error when I run it:
Use of uninitialized value within %word_count in concatenation (.) or string
at perl.pl line 26, <IN> line 520.
I don't understand why any value would be unintialized in that hash as the
loops previous should have taken care of all the initializing and word
counts. If I remove the last foreach loop and just put a common word for
ex.: print "$word_count{the}\n" I get the count for that word.
Thanks,
Jon