Forum: Ruby convert string to array with delimiter

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Af3cecc8af253b5acd3c09c5b67c0074?d=identicon&s=25 Valentino Lun (on9west)
on 2008-11-05 10:06
Dear all

I have the following string want to convert to arry

str="/dev/lis_home_712     2097152   1052656   1044496      51%
/appl/lis/home2\n"

I use str.scan(/\w+/), which give the following result
["dev", "lis_home_712", "2097152", "1052656", "1044496", "51", "appl",
"lis", "home2"]

However, I expect the result like this
["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
"/appl/lis/home2"]

Please advise on how to do it. Thank you.
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2008-11-05 10:11
(Received via mailing list)
2008/11/5 Valentino Lun <sumwo@yahoo.com>:
> I use str.scan(/\w+/), which give the following result

str.split( ' ' )

Farrel
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2008-11-05 10:14
(Received via mailing list)
From: Valentino Lun [mailto:sumwo@yahoo.com]
# However, I expect the result like this
# ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
# "/appl/lis/home2"]
#
# Please advise on how to do it. Thank you.

read on string fxns, qri String#.
here in this case you may use String#split

try first,

  str.split

(note w/out an arg)
666b4e17b4bb0e2d999037a25f65a7cb?d=identicon&s=25 Heesob Park (phasis)
on 2008-11-05 10:18
(Received via mailing list)
2008/11/5 Valentino Lun <sumwo@yahoo.com>:
>
> However, I expect the result like this
> ["dev/lis_home_712", "2097152", "1052656", "1044496", "51%",
> "/appl/lis/home2"]
>
> Please advise on how to do it. Thank you.

str.scan(/\S+/)
or
str.split(/\s+/)
or
str.split

Regards,

Park Heesob
This topic is locked and can not be replied to.