There are a few style related things that may help. First, always use strictures (use strict; use warnings;). They give an early warning about a number of common types of error.

You hardly ever need to explicitly count things in Perl so you don't need to maintain $wordcount in the loop.

Always use the three parameter open. It's safer and explicit about the open mode. Good to see you testing the open result by the way!

Keep variables as local as possible. Note in the sample below that $deadstring is local to the loop (why that name btw?).

Your off by one in $wordcount was probably due to a blank line. Better to test for that in the loop.

Here's a reworked version of your code. Note that the "file" is actually a string variable. That's a really useful trick for samples like this so you don't need a "real" file.

use strict; use warnings; my $wordlist = <<WORDS; a the cat mat sat WORDS my $userinput = "myexample"; my $wordfile = '/home/psychohamster/Desktop/Desktop_Stuff/AnagramWor +dList'; my %hashlist; open my $wordhandle, '<', \$wordlist or die "Unable to open WordList: +$!"; while (my $deadstring = <$wordhandle>) { chomp $deadstring; next unless length $deadstring; $hashlist{$deadstring} = 1; } my $wordCount = keys %hashlist; if ($wordCount) { print "Found $wordCount words:\n"; print join "\n", sort keys %hashlist, ''; } else { print "Didn't find any words\n"; }

Prints:

Found 5 words: a cat mat sat the

Perl is environmentally friendly - it saves trees

In reply to Re: Attempting to fill a hash by GrandFather
in thread Attempting to fill a hash by TwistedTransistor

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.