in reply to How can I use regex to copy each word of a string into a hash?

Are you sure you don't mean an array? How would you break the words up into keys and values? If you just want to stuff values into the keys of the hash (with '1' as the value), the basic form looks like this:
my $sentence = 'This is a test'; my %hash = map { $_, 1 } ($sentence =~ /\w+/g);
If you need a count, you could do this:
my $sentence = 'This is a test'; my %hash; for ($sentence =~ /\w+/g) { $hash{ $_ }++; }

Replies are listed 'Best First'.
Re: Re: How can I use regex to copy each word of a string into a hash?
by cei (Monk) on Jan 16, 2001 at 23:42 UTC
    A frequency count is exactly the direction I was headed. Perl Cookbook gave an example for "Processing Every Word in a File", but it wasn't clear to me how to apply it to a string instead of a file.

    thanks