converting a string to a do-able.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

converting a string to a do-able.

joe biggins
Hi,

I am a total newby so I am probably missing something obvious.  I am trying to make a small program where the user can type in simple formula as a text string and get the answer.  So '3 plus 5' gives the answer 8.  I am having trouble converting the 'plus' into +.  Is there as asNumber equivalent for maths symbols or anything like that?

Sorry if this question makes no sense.

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: converting a string to a do-able.

Bert Freudenberg

On 10.12.2010, at 16:00, joe biggins wrote:

> Hi,
>
> I am a total newby so I am probably missing something obvious.  I am trying to make a small program where the user can type in simple formula as a text string and get the answer.  So '3 plus 5' gives the answer 8.  I am having trouble converting the 'plus' into +.  Is there as asNumber equivalent for maths symbols or anything like that?

There is asSymbol, which would convert the string '+' into the Symbol #+. That can be send as a message using the perform method. But since you want to go from 'plus' to #+ you can do this directly using a lookup-table:

        string := '3 plus 5'.
        operators := {'plus' -> #+} as: Dictionary.
        tokens := string findTokens: Character space.
        objects := tokens collect: [:token | operators at: token ifAbsent: [token asNumber]].
        objects first perform: objects second with: objects third

This first splits the input string at spaces, so you get an array containing the strings '3', 'plus', and '5'.

Then these are converted to numbers and symbols by look-up in a dictionary that maps 'plus' to #+, and uses #asNumber if not found. This results in an array of the objects 3, #+, and 5.

Then you can send the "+" message to the number object "3" with the number object "5" as argument. The result is the number object "8".

To see what's going on, do the above in a workspace and inspect the "operators", "tokens", and "objects" variables.

If you do not insist on using the word 'plus', you can let the Compiler do the job of converting the string to number objects and symbols and sending them as messages:

        Compiler evaluate: '3 + 5'

Also, if you really want to use 'plus' then some string replacement would work:

        input := '3 plus 5'.
        Compiler evaluate: (input copyReplaceTokens: 'plus' with: '+')

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: converting a string to a do-able.

joe biggins
In reply to this post by joe biggins
Thanks Joe, that's just what I was looking for. Fantastic!! Thanks again.

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners