At 11:17 AM 4/24/2001, you wrote:
>so...this is suposed to count the words in FILE and return how many 
>occourances of each word there were...its not working for me though....its 
>only returning the count for the last word in the file...help
>
>#!/usr/local/bin/perl
>
>open (FILE,"../www/main.php3");
>@lines=<FILE>;
>close(FILE);
>
>foreach $i (@lines) {
>@words = split(/\s+/, $i);
>}

The loop above clobbers @words each time.  The loops below only work on the 
last line of the file.

>foreach $word (@words) {
>$wordcount{"$word"}=0;
>}

You're initializing every entry in the hash to 0, and you don't have to do 
that.  The rest of the code looks good. But we still have to deal with the 
problem of not getting all of the words into @words.

I think this would work, if used as the first loop of the program.

foreach (@lines) {
     push @word, split /\s+/;
}

With the loop above, as you split words out of @lines, they get _added_ to 
@words.  The assignment operator, =, would clobber the value already there, 
leaving us with just the last thing we assigned to it, the last line of the 
file.  Look in perlfunc for the push, pop, shift, and unshift 
functions.  If you don't know about stacks and queues, get a decent book on 
data structures and check them out.  They are amazingly powerful for how 
simple they are, and Perl is nice enough to have all the stuff built in so 
you can treat regular arrays like either (or both) of them.

As for hashes, when you use a hash key for the first time, the value is 
undef.  Undef, in a numeric context, looks like 0.  So all we have to do is 
add 1 for each time we see a word, and we are ok.  There's no need to 
initialize them all to zero.  So your program would become something that 
looks like this.

use strict;

open FILE, "../www/main.php3" or die "Can't open the file: $!";
# I added the "or die ..." above, because you want your
# program to halt if you have no data to work on.  You
# also want to check the return values of functions that
# do stuff outside of your program, to make sure that
# the succeed.
@lines=<FILE>;
close(FILE);

foreach (@lines) {
     push @words, split /\s+/;
}

my %wordcount = ();

foreach my $word (@words) {
     # Oh, my $word!  : )
     $wordcount{$word}++;
     # $var++ is shorthand for $var += 1
     # but either is fine.
}

But, this can be cleaned up further!  You can compress it all down to one 
loop, in a few different ways.  Take another look at it Chris, play with 
it, and crunch it down.  Less code, less bugs, less stuff to worry about.

Good luck!

Thank you for your time,

Sean.

Reply via email to