in reply to minimal response program code problem
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 | |
by johngg (Canon) on Dec 05, 2006 at 23:33 UTC |