Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: putting text into array word by word

by jms53 (Monk)
on Jan 09, 2012 at 18:55 UTC ( #947059=note: print w/ replies, xml ) Need Help??


in reply to Re^3: putting text into array word by word
in thread putting text into array word by word

PERFECT, Thank you sooo much

I can now go back to calmly pseudo-geeking.


Comment on Re^4: putting text into array word by word
Re^5: putting text into array word by word
by Not_a_Number (Parson) on Jan 09, 2012 at 20:11 UTC

    OK, now that's solved, let's look at the definition of 'word' (yes, things are going to get hairy...). Take this sentence, for example:

    "No, he said."

    The 'words' that your current code would extract are:

    "No, he said."

    Is that really what you want? Or would you prefer:

    No # or, better, 'no' he said

    ?

      during the foreach loop I remove punctuation and set to lowercase (such that No and no are the same word.
      while (<FILE>) { my @these_words = split(' ', $_); foreach my $this_word (@these_words) { $this_word =~ s/[[:punct:]]//g; $this_word = lc($this_word); push @all_words, $this_word; } }
        $this_word =~ s/[[:punct:]]//g;

        The only problem with that approach is that it removes internal punctuation (ie apostrophes) as well, so that I'll becomes ill, she'd becomes shed, etc. ('Why was Virgina Woolf so obsessed with sheds?' I hear someone ask.)

        I'd use this instead:

        $this_word =~ s/^[[:punct:]]+//; # Remove leading punct. $this_word =~ s/[[:punct:]]+$//; # Remove trailing punct.

        Update: added Virginia Woolf sentence.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (11)
As of 2014-10-21 22:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (112 votes), past polls