Dear all, I'm a newbie to ruby and today when I'm writing a simple
script to process some data, I found something I can't understand.
The data is stored in several column seperated by tab or space. I use
the following code to get the data (assuming the data comes from
standard input and all numbers are integer)
data=[]
counter = 0
while line = STDIN.gets
data[counter] = line.split
data[counter].map! {|str| str.to_i}
counter += 1
end
hence data[i] is an array hold all the numbers in the ith line, data[i]
[j] is the number on ith line and jth column. Then what I want the
script to do is sorting lines according to a specified column. I
thought the following code should work:
result = data.sort {|x, y| x[col] <=> y[col] }
where the col determine which column the script will sort according
to. However ruby raise a error saying:
"undefined method `<=>' for nil:NilClass (NoMethodError)
from ana.rb:16:in `sort'
from ana.rb:16
"
I have to write the code as
result = data.sort {|x, y| x[col].to_i <=> y[col].to_i }
to let the script run properly. I'm quite confused here. I think the
elements of array data are converted to integer when the code
data[counter].map! {|str| str.to_i}
finished. However why ruby still requires a explicit conversion when I
use the data.sort?
Yes, they are. You might be doing some typo or other crap. Following
program runs verbatim:
data=
counter = 0
while line = STDIN.gets
data[counter] = line.split
data[counter].map! {|str| str.to_i}
counter += 1
end
#p data.sort_by { |x| x[2] } #=> you can use sort_by as an alternative
p data.sort {|x,y| x[2] <=> y[2] } #=> This also work.
···
On 4/21/07, Larme <lalalarme@gmail.com> wrote:
Dear all, I'm a newbie to ruby and today when I'm writing a simple
script to process some data, I found something I can't understand.
The data is stored in several column seperated by tab or space. I use
the following code to get the data (assuming the data comes from
standard input and all numbers are integer)
data=
counter = 0
while line = STDIN.gets
data[counter] = line.split
data[counter].map! {|str| str.to_i}
counter += 1
end
hence data[i] is an array hold all the numbers in the ith line, data[i]
[j] is the number on ith line and jth column. Then what I want the
script to do is sorting lines according to a specified column. I
thought the following code should work:
result = data.sort {|x, y| x[col] <=> y[col] }
where the col determine which column the script will sort according
to. However ruby raise a error saying:
"undefined method `<=>' for nil:NilClass (NoMethodError)
from ana.rb:16:in `sort'
from ana.rb:16
"
I have to write the code as
result = data.sort {|x, y| x[col].to_i <=> y[col].to_i }
to let the script run properly. I'm quite confused here. I think the
elements of array data are converted to integer when the code
data[counter].map! {|str| str.to_i}
finished. However why ruby still requires a explicit conversion when I
use the data.sort?
result = data.sort {|x, y| x[col].to_i <=> y[col].to_i }
to let the script run properly. I'm quite confused here. I think the
elements of array data are converted to integer when the code
data[counter].map! {|str| str.to_i}
finished. However why ruby still requires a explicit conversion when I
use the data.sort?
If you have an array arr with n elements which you all turn into integers,
arr[i] with i>=n will still be nil. So if in your case not all rows have more
than col columns, that would explain the problem.
HTH,
Sebastian Hungerecker
···
--
NP: Kataklysm - Where the Enemy Sleeps...
Ist so, weil ist so
Bleibt so, weil war so
If you want to be sure that you extract only integers you could do
l.scan(/\d+/).map {|x| x.to_i}
instead of the split.
Lots of options...
Kind regards
robert
···
On 21.04.2007 20:16, Larme wrote:
Dear all, I'm a newbie to ruby and today when I'm writing a simple
script to process some data, I found something I can't understand.
The data is stored in several column seperated by tab or space. I use
the following code to get the data (assuming the data comes from
standard input and all numbers are integer)
data=
counter = 0
while line = STDIN.gets
data[counter] = line.split
data[counter].map! {|str| str.to_i}
counter += 1
end
hence data[i] is an array hold all the numbers in the ith line, data[i]
[j] is the number on ith line and jth column. Then what I want the
script to do is sorting lines according to a specified column. I
thought the following code should work:
result = data.sort {|x, y| x[col] <=> y[col] }
where the col determine which column the script will sort according
to. However ruby raise a error saying:
"undefined method `<=>' for nil:NilClass (NoMethodError)
from ana.rb:16:in `sort'
from ana.rb:16
"
I have to write the code as
result = data.sort {|x, y| x[col].to_i <=> y[col].to_i }
to let the script run properly. I'm quite confused here. I think the
elements of array data are converted to integer when the code
data[counter].map! {|str| str.to_i}
finished. However why ruby still requires a explicit conversion when I
use the data.sort?
Thank all of you. Yes, the problem is that the column number is not
fixed -- it should be, but my friend who prepare the data made some
thing wrong.
···
On Apr 22, 3:25 am, Robert Klemme <shortcut...@googlemail.com> wrote:
On 21.04.2007 20:16, Larme wrote:
> Dear all, I'm a newbie to ruby and today when I'm writing a simple
> script to process some data, I found something I can't understand.
> The data is stored in several column seperated by tab or space. I use
> the following code to get the data (assuming the data comes from
> standard input and all numbers are integer)
> data=
> counter = 0
> while line = STDIN.gets
> data[counter] = line.split
> data[counter].map! {|str| str.to_i}
> counter += 1
> end
> hence data[i] is an array hold all the numbers in the ith line, data[i]
> [j] is the number on ith line and jth column. Then what I want the
> script to do is sorting lines according to a specified column. I
> thought the following code should work:
> result = data.sort {|x, y| x[col] <=> y[col] }
> where the col determine which column the script will sort according
> to. However ruby raise a error saying:
> "undefined method `<=>' for nil:NilClass (NoMethodError)
> from ana.rb:16:in `sort'
> from ana.rb:16
> "
> I have to write the code as
> result = data.sort {|x, y| x[col].to_i <=> y[col].to_i }
> to let the script run properly. I'm quite confused here. I think the
> elements of array data are converted to integer when the code
> data[counter].map! {|str| str.to_i}
> finished. However why ruby still requires a explicit conversion when I
> use the data.sort?
You probably have lines with differing number of entries and thus
sometimes x[col] just returns nil.