[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string to substring??

Pat Kiatchaipipat

12/3/2007 2:15:00 PM

I have one string name

message = "5x-2=18"

and I want to create substring or array by this

mesArray[0] = 5
mesArray[1] = x
mesArray[2] = -
...

this has function?
how can I make it?
--
Posted via http://www.ruby-....

3 Answers

Ilan Berci

12/3/2007 2:19:00 PM

0

Pat Kiatchaipipat wrote:
> I have one string name
>
> message = "5x-2=18"
>
> and I want to create substring or array by this
>
> mesArray[0] = 5
> mesArray[1] = x
> mesArray[2] = -
> ...
>
> this has function?
> how can I make it?

irb(main):001:0> mesArray = "5x-2=18".split ""
=> ["5", "x", "-", "2", "=", "1", "8"]
--
Posted via http://www.ruby-....

Robert Klemme

12/3/2007 2:23:00 PM

0

2007/12/3, Pat Kiatchaipipat <l3litzer@hotmail.com>:
> I have one string name
>
> message = "5x-2=18"
>
> and I want to create substring or array by this
>
> mesArray[0] = 5
> mesArray[1] = x
> mesArray[2] = -
> ...
>
> this has function?
> how can I make it?

You have that already in your String:

irb(main):001:0> message = "5x-2=18"
=> "5x-2=18"
irb(main):002:0> message[0].chr
=> "5"
irb(main):003:0> message[1].chr
=> "x"
irb(main):004:0> message[2].chr
=> "-"

I am suspecting though that what you really want is a parser for
expressions. A simplistic version could look like this:

irb(main):005:0> message.scan %r{\d+|\w+|[-+=]}
=> ["5", "x", "-", "2", "=", "18"]

If you want to properly process expressions with brackets etc. you
need a more complex solution.

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Pat Kiatchaipipat

12/3/2007 2:44:00 PM

0

thank you robert :D
--
Posted via http://www.ruby-....