in reply to Parsing user input for AIM bot

an AIM bot uses perl to logon to Aol Instant Messenger. It responds to users that instant messenge it. The data is a scalar. It is text. $msg. It might equal "How are you?" or something like that. It is all fine and dandy until someone says "How are u". The bot doesn't know how to respond to that. I would need to be able to parse the "how" in "How are u" into $parsed and the are in "How are you" into $parsed2 or something like that so I could say

if($parsed eq "how" && $parsed2 eq "are"){
$msg = "I'm fine. How are you?"
}

and give a responce. So basically what I need is to parse $msg at every space and create a variable for each one. Or something like that. Any suggestions? Thanks

Replies are listed 'Best First'.
Re: Re: Parsing user input for AIM bot
by The Mad Hatter (Priest) on Mar 09, 2003 at 05:25 UTC
    I'm not sure that what you described above is the best solution to your problem, but this will take the message and shove the words into an array:
    @words = split /\s+/, <DATA> print join "\n", @words, ""; __DATA__ How are you?
    That prints:
    How are you?
    Note that the last word contains punctuation. To remove punctuation, change the first line to:
    map { s/[\?\.\!]*//g; } (@words = split /\s+/, <DATA>);
    Update: Added bit about punctuation.

    Update 2: Without a more thorough description or code to look at, a better solution might be to just replace common abbreviations with the full word. For example:
    $msg =~ s/\bu\b/you/g; # Replace "u" with "you" $msg =~ s/\br\b/are/g; # Replace "r" with "are" ...