Soma-Cube

C

CBlair1986

I'm looking to take something like
/123/456/789
/0ab/cde/fgh
/ijk/lmn/opq

and turn it into an array like
[["123","456","789"][...][...]]

everything besides the /'s will be digits. I've been trying to solve it
using regular expressions, but I'm still very unfamiliar with them.
Also, there may be more or less '/###' sections than three, and they
might contain more than or less than three digits.

Any help is greatly appreciated!
 
G

Greg Brown

irb(main):001:0> "/123/456/789/0ab/cde/fgh/ijk/lmn/opq".split("/")
=> ["", "123", "456", "789", "0ab", "cde", "fgh", "ijk", "lmn", "opq"]

HTH.
 
G

Greg Brown

Michael said:
Or:

your_input.split.map {|line| line.split('/')[1..-1] }

Yeah, this is probably what you're looking for. A quick explanation of
whats going on is the input is being split by line, and then 'mapped'
so that each line becomes an array split by the /'s and the [1..-1]
gives you everything except for the first empty string on the left side
of your first /

so this should give you your two dimensional array.
 
M

Mark Van Holstyn

------=_Part_33990_22679445.1128923012693
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

You may want to add a .flatten on the end of that, otherwise if you have
multiple lines of input, you will get an array containing an array for each
line.

Mark

Michael said:
Or:

your_input.split.map {|line| line.split('/')[1..-1] }

Yeah, this is probably what you're looking for. A quick explanation of
whats going on is the input is being split by line, and then 'mapped'
so that each line becomes an array split by the /'s and the [1..-1]
gives you everything except for the first empty string on the left side
of your first /

so this should give you your two dimensional array.


--
Mark Van Holstyn
(e-mail address removed)
http://lotswholetime.com

------=_Part_33990_22679445.1128923012693--
 
C

CBlair1986

Greg said:
Yeah, this is probably what you're looking for. A quick explanation of
whats going on is the input is being split by line, and then 'mapped'
so that each line becomes an array split by the /'s and the [1..-1]
gives you everything except for the first empty string on the left side
of your first /

so this should give you your two dimensional array.

Yes, that's exactly what I wanted. Thanks, you all.

And it's so simple, too.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,975
Members
47,533
Latest member
medikillz39

Latest Threads

Top