Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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 ( [id://52320]=note: print w/replies, xml ) Need Help??


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

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?
  • Comment on Re: Re: How can I use regex to copy each word of a string into a hash?

Replies are listed 'Best First'.
If you just want words...
by tedv (Pilgrim) on Jan 17, 2001 at 01:37 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://52320]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found