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

Keep in mind that split will generally be a more efficient solution, whenever it is possible.

When's the last time you used duct tape on a duct? --Larry Wall
  • Comment on Re: How can I use regex to copy each word of a string into a hash?
  • Download Code

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:45 UTC
    True, but if I'm trying to index english text, the oddities of punctuation may make a split impractical. Unless you've got a suggestion on an iterative split?
      If you just want words, why bother with punctuation at all? Just do a massive s/\W+/ /g on the string beforehand and you'll get a big list of words, separated by spaces. I suppose those damn apostrophies will cause you pain, and you want "it's" to differ from "its". It's unclear whether or not capitalization matters-- is "BASIC" a different word from "basic"? What about "Smith" versus "smith"?

      Anyway, I'd probably write something like this:
      local $/ = undef; $_ = <MY_FILE>; my %hash = (); $hash{$_}++ foreach split /[^\w']+/; # Change $_ to lc if case matters


      -Ted