[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Parse/Split `cmd`

tjones

8/13/2006 1:27:00 AM

Hello,
I'm really new to Ruby, I have been using TCL for a really long time
and I just picked up the "Programming Ruby" book and I really like it.
One very simple thing I do a lot in my TCL scripts is parse the output
from a cmd it would read [split $x \n].

OK, I have a def defined to run my command line app and I want to split
the result at the line ending, how do I do it? I have tried split(\n)
and I get a error.

Thanks,
tom

2 Answers

Suraj Kurapati

8/13/2006 1:36:00 AM

0

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

tjones wrote:
> One very simple thing I do a lot in my TCL scripts is parse the output
> from a cmd it would read [split $x \n].

Use $x.split, which works on newlines by default.

> OK, I have a def defined to run my command line app and I want to split
> the result at the line ending, how do I do it? I have tried split(\n)
> and I get a error.

irb(main):001:0> s = "foo\nbar"
=> "foo\nbar"

irb(main):002:0> s.split
=> ["foo", "bar"]

irb(main):003:0> s.split "\n"
=> ["foo", "bar"]

irb(main):004:0> s.split '\n'
=> ["foo\nbar"]

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (GNU/Linux)

iD8DBQFE3oG1mV9O7RYnKMcRArdTAKCVfaDz+7c+7NEmANm0Jmyf3bmeFgCgk5SP
r210B4lLIt295WThfDYoQdU=
=WWlR
-----END PGP SIGNATURE-----

Nobuyoshi Nakada

8/13/2006 1:59:00 AM

0

Hi,

At Sun, 13 Aug 2006 10:30:10 +0900,
tjones wrote in [ruby-talk:208085]:
> One very simple thing I do a lot in my TCL scripts is parse the output
> from a cmd it would read [split $x \n].

lines = `cmd`.to_a

lines = IO.popen("cmd") {|f| f.readlines}

--
Nobu Nakada