On 6 Dec 2007, at 09:38, Venks wrote:
ccc, 30, test3
ddd, 40, test4
I want to create a 2 dimensional array with each line being an array.
lines = cat /tmp/test.txt
will create a single dimensional array
by default.
I’m not so sure about that last comment. As far as I know backticks
always return a String.
The easiest way to do what you want is just to split the String from
backticks by line and then each line by whatever delimiter you chose.
You can do this with map for instance:
[alexg@powerbook]/Users/alexg/Desktop(47): cat test.txt
aaa, 10, test1
bbb, 20, test2
ccc, 30, test3
ddd, 40, test4
[alexg@powerbook]/Users/alexg/Desktop(48): cat capture.rb
p cat #{ARGV[0]}
.split(/\n/).map{|line| line.split(/, /)}
[alexg@powerbook]/Users/alexg/Desktop(49): ruby capture.rb test.txt
[[“aaa”, “10”, “test1”], [“bbb”, “20”, “test2”], [“ccc”, “30”,
“test3”], [“ddd”, “40”, “test4”]]
Alex G.
Bioinformatics Center
Kyoto University