in reply to The read function and newlines

Adetque:

I don't see a way for a newline to be in your list. However, it looks like you'll get an empty string in your hash: You read the file with a newline, the newline is ignored, then you hit the end of the file. So $currentWord is "", which doesn't exist in your hash, so it's added. You'll probably want to verify that $currentWord isn't empty before stuffing it into your hash.

Having said that, though, I think I'd just use split to get your list of words and enter them into the hash--something like this (untested):

sub readWords { ## Gets how many of each word are in a file and returns a hash my $file = shift; my %words = (); # What characters to ignore my $blacklist = qr{[\s~`!@#\$%\^&\*\(\)\{\}\+=\\\/\[\]\.\,<>\?;:"] ++}; open(my $FILE, "<", $file) or die("$0: $file: $!\n"); while(my $currentline = <$FILE>) { $words{$_}++ for split $blacklist, $currentline; } close($FILE); return %words; }

...roboticus

Update: I just tested the function and it works. A couple observations, though:

Replies are listed 'Best First'.
Re^2: The read function and newlines
by Adetque (Initiate) on Jul 03, 2010 at 20:23 UTC

    Thanks. Both the split function and deleting the key work.

    Also, I purposely didn't remove apostrophes since they could be part of a word, and I just forgot about underscores.

      Also, I purposely didn't remove apostrophes since they could be part of a word, and I just forgot about underscores.

      Heh ... I hadn't even considered contractions. I'm glad it's working for you!

      ...roboticus