I declared a hash and then used a hash slice to set all of the keys in the has equal to the values of the array and all of the hash values equal to 'undef'. If you'd like to see it better, you can add these two lines after this code:
use Data::Dumper;
print Dumper \%nocapslist;
That produces:
$VAR1 = {
'nor' => undef,
'with' => undef,
'in' => undef,
'or' => undef,
'about' => undef,
'that' => undef,
'as' => undef,
'the' => undef,
'and' => undef,
'for' => undef,
'it' => undef,
'but' => undef,
'to' => undef,
'because' => undef
};
Also, note that we're really not "setting" the values to undef. That's just setting the first hash value to undef and the rest default to that. If you wanted to set all of the values to 1, for example, you'd do something like this:
@nocapslist{ @nocapslist } = (1) x @nocapslist;
There are two benefits to using a hash instead of a grep. The first is that the hash lookup is faster. I don't really consider that a benefit, though, because it's always better to optimize for clarity than speed. I changed it to a hash for the second reason: using exists is much easier than using a grep, in terms of programmers understanding it.
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats. |