in reply to Word Counting

Wow... when is this homework due? ;)

1. Open the file
2. Read it line by line
3. Split each line into words (on spaces)
4. Add each word to a hash (this will ensure uniqueness)
5. When all lines are processed, print hash keys


--perlplexer

Replies are listed 'Best First'.
Re: Re: Word Counting
by samurai (Monk) on Apr 24, 2003 at 21:02 UTC
    I would reccomend $wordhash{lc $word}++; as opposed to $wordhash{$word}++; if you're not worried about case-sensitivity, so you don't end up with:

    To: 50
    to: 23

    Also, if you're going to split on \s+ as everyone seems to be suggesting, don't forget you're going to have to $word =~ s/\W//g to get rid of those unsightly punctuation marks.

    --
    perl: code of the samurai

Re: Re: Word Counting
by Anonymous Monk on Apr 24, 2003 at 20:53 UTC
    This definately isn't homework, heh...sometimes I wish I were still in school though. I know how to open and read files, but I don't understand part #3. How do I split on whitespace? You'd have to split each word into their own variable, that's the part I don't understand.
      You can use split to put the words into an array or a list:
      my $line = "This is a line of text"; my @words = split /\s+/, $line;
      Note that the /\s+/ bit technically isn't needed, because split defaults to splitting on whitespace.

      There's more info on split on Perldoc.

      Hope that helps,
      -- Foxcub
      A friend is someone who can see straight through you, yet still enjoy the view. (Anon)

      Use split(); e.g.,
      # Assuming %words is the hash where you keep all the words $words{$_}++ for split /\s+/, $line;
      --perlplexer