in reply to minimal response program code problem

jbert and liverpole have given you some pointers to get you further along. This post is to show you how to save a lot of typing when setting up your %Goodwords hash. Since all you are storing for values in the hash is 1, you can set all of the keys up first in an array and then use a map or a foreach loop to set up the hash. Also, you can use the quote words operator qw{ ... } to set up those keys that don't contain spaces to save keep typing quote marks. First let's set up the list of keys

use strict; use warnings; # Set up the keys that contain no spaces. # my @listOfGoodwords = qw{ mhm right well yeah sure good ah okay yep hm definitely alright 'm'm oh my god wow uhuh exactly yup mkay ooh cool uh fine true hm'm hmm yes absolutely great um so mm weird huh yay maybe eh obviously correct awesome really interesting ye-}; # Now add those keys that do contain spaces. # push @listOfGoodwords, q{i see}, q{i mean}, q{i know}, q{i think so};

Note that the q{ ... } is the same as using single-quotes whereas qq{ ... } is the same as double-quotes. Variables and character escapes like \n for newline interpolate in double-quotes but not in single-quotes. It is good practice only to use double-quotes when you require interpolation and to use single-quotes at all other times.

Now that we have the list we construct the hash, either with a foreach

my %Goodwords; foreach my $key (@listOfGoodwords) { $Goodwords{$key} = 1; }

or with a map

my %Goodwords = map {$_ => 1} @listOfGoodwords;

The map might look a little strange but all it is doing is passing each key in @listOfGoodwords into the map from the right (items passed into a map are held in the $_ variable) and passing two things, the key and value 1, out of the map to the left, thus populating the hash.

I hope this is of use to you.

Cheers

JohnGG

Update: Corrected typo in explanation of the map

Replies are listed 'Best First'.
Re^2: minimal response program code problem
by GrandFather (Saint) on Dec 05, 2006 at 22:31 UTC

    I quite often use a hash splice in that context:

    my %Goodwords; @Goodwords{@goodWordsList} = (1) x @listOfGoodwords;

    TIMTOWTDI and YMMV. ;-)


    DWIM is Perl's answer to Gödel
      Nice. GrandFather ++

      I've seen something like this in books a couple of times recently but it hasn't registered yet in my grab-bag of handy tools. Mainly because I still think of x as just a string multiplier and not as a list multiplier as well. I need to use it a couple of times so that it springs to mind on cue.

      Cheers,

      JohnGG