How do I add?

I've got a file that is in two columns, how do I add the second column
up as I read through the file?

Code:
#!/usr/bin/ruby
file = File.open('some_file.txt')

# Going through each line
file.each do |line|
  data = line.split("\t")
  total += data[1] # How do I make this line work?
end

puts total

Hello,

you read strings from the file, but you want to add numbers. Therefore
you need to convert the strings into numbers first.

Take a look at the documentation of String#to_i or String#to_f, for example.

cu,
Thomas

···

2009/8/7 chutsu <chutsu@gmail.com>:

I've got a file that is in two columns, how do I add the second column
up as I read through the file?
[...]

--
When C++ is your hammer, every problem looks like your thumb.

I've got a file that is in two columns, how do I add the second column
up as I read through the file?

Code:
#!/usr/bin/ruby
file = File.open('some_file.txt')

total = 0 # make the variable have scope outside the block

# Going through each line
file.each do |line|
data = line.split("\t")
total += data[1] # How do I make this line work?

# and convert the string to a numeric depending on what you expect
total += data[1].to_f
total += data[1].to_i

end

puts total

Or you could:

puts File.readlines('some_file.txt').inject(0) {|total,line| total + line.split("\t")[1].to_i }

(but if the file is large, that could be terribly inefficient)

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Aug 7, 2009, at 12:50 PM, chutsu wrote:

awk "{t += $2} END {print t}" some_file.txt

···

On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

I've got a file that is in two columns, how do I add the second column
up as I read through the file?

Code:
#!/usr/bin/ruby
file = File.open('some_file.txt')

# Going through each line
file.each do |line|
  data = line.split("\t")
  total += data[1] # How do I make this line work?
end

puts total

Hello,

inject is a method of Iterable, so it's unnecessary to read the whole
file into memory first to use it. I would prefer a solution that is
both elegant and efficient:

  puts file.inject(0) {|acc, line| acc + line.split(/\t/)[1].to_f }

cu,
Thomas

···

2009/8/7 Rob Biedenharn <Rob@agileconsultingllc.com>:

[...]
puts File.readlines('some_file.txt').inject(0) {|total,line| total +
line.split("\t")[1].to_i }

(but if the file is large, that could be terribly inefficient)
[...]

--
When C++ is your hammer, every problem looks like your thumb.

ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

···

On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:

On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

> I've got a file that is in two columns, how do I add the second column
> up as I read through the file?

> Code:
> #!/usr/bin/ruby
> file = File.open('some_file.txt')

> # Going through each line
> file.each do |line|
> data = line.split("\t")
> total += data[1] # How do I make this line work?
> end

> puts total

awk "{t += $2} END {print t}" some_file.txt

[...]
puts File.readlines('some_file.txt').inject(0) {|total,line| total +
line.split("\t")[1].to_i }

(but if the file is large, that could be terribly inefficient)
[...]

Hello,

inject is a method of Iterable, so it's unnecessary to read the whole

I think you meant Enumerable

file into memory first to use it. I would prefer a solution that is
both elegant and efficient:

puts file.inject(0) {|acc, line| acc + line.split(/\t/)[1].to_f }

cu,
Thomas

-- When C++ is your hammer, every problem looks like your thumb.

Maybe "Iterable" is something from C++? :wink:

I guess I could have simply replaced .readlines with .open in my one-liner.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Aug 7, 2009, at 2:27 PM, Thomas Chust wrote:

2009/8/7 Rob Biedenharn <Rob@agileconsultingllc.com>:

Thomas Chust wrote:

I would prefer a solution that is
both elegant and efficient:

Then you would runaway from inject.

···

--
Posted via http://www.ruby-forum.com/\.

Just because you can still write your Perl in Ruby, doesn't mean that you *should*.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Aug 7, 2009, at 8:10 PM, w_a_x_man wrote:

On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:

On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

I've got a file that is in two columns, how do I add the second column
up as I read through the file?

Code:
#!/usr/bin/ruby
file = File.open('some_file.txt')

# Going through each line
file.each do |line|
data = line.split("\t")
total += data[1] # How do I make this line work?
end

puts total

awk "{t += $2} END {print t}" some_file.txt

ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

[...]
inject is a method of Iterable, so it's unnecessary to read the whole

I think you meant Enumerable

Hello,

you're right, of course. Writing programs in Java and Scheme in
parallel and replying to a Ruby thread in between apparently caused
some mixup of contexts in my head :wink:

[...]
-- When C++ is your hammer, every problem looks like your thumb.

Maybe "Iterable" is something from C++? :wink:

*g*

I think, the C++ STL has "iterator", "Iterable" can, for example, be
found in Java...

cu,
Thomas

···

2009/8/7 Rob Biedenharn <Rob@agileconsultingllc.com>:

On Aug 7, 2009, at 2:27 PM, Thomas Chust wrote:

--
When C++ is your hammer, every problem looks like your thumb.

just ignore waxman, he's a known troll.

···

On Aug 7, 8:43 pm, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:

On Aug 7, 2009, at 8:10 PM, w_a_x_man wrote:

> On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:
>> On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

>>> I've got a file that is in two columns, how do I add the second
>>> column
>>> up as I read through the file?

>>> Code:
>>> #!/usr/bin/ruby
>>> file = File.open('some_file.txt')

>>> # Going through each line
>>> file.each do |line|
>>> data = line.split("\t")
>>> total += data[1] # How do I make this line work?
>>> end

>>> puts total

>> awk "{t += $2} END {print t}" some_file.txt

> ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

Just because you can still write your Perl in Ruby, doesn't mean that
you *should*.

-Rob

Rob Biedenharn http://agileconsultingllc.com
R...@AgileConsultingLLC.com

An example from Matz (Ruby in a Nutshell):

ruby -ne 'print if /Ruby/' /usr/share/dict/words

Matz put these features in Ruby so that they could be used,
not so that they would not be used.

But in your blind arrogance, you believe that because of your
very special two-digit IQ, you are the sole arbiter of what is
permissible with Ruby. And when someone dares to post code that
does not conform to your standards of bloat, pretentiousness,
pomposity, inefficiency, and intellectual mediocrity, you lash
out with self-righteous savagery.

As a man and as a programmer, you are a sick joke.

···

On Aug 7, 7:43 pm, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:

On Aug 7, 2009, at 8:10 PM, w_a_x_man wrote:

> On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:
>> On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

>>> I've got a file that is in two columns, how do I add the second
>>> column
>>> up as I read through the file?

>>> Code:
>>> #!/usr/bin/ruby
>>> file = File.open('some_file.txt')

>>> # Going through each line
>>> file.each do |line|
>>> data = line.split("\t")
>>> total += data[1] # How do I make this line work?
>>> end

>>> puts total

>> awk "{t += $2} END {print t}" some_file.txt

> ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

Just because you can still write your Perl in Ruby, doesn't mean that
you *should*.

Please ignore this pitiable fellow. He is a well-known
lick-spittle, toad-eater, dimwit, poltroon, catamite, sycophant,
and microcephalic idiot.

···

On Aug 7, 7:48 pm, pharrington <xenogene...@gmail.com> wrote:

On Aug 7, 8:43 pm, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:

> On Aug 7, 2009, at 8:10 PM, w_a_x_man wrote:

> > On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:
> >> On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

> >>> I've got a file that is in two columns, how do I add the second
> >>> column
> >>> up as I read through the file?

> >>> Code:
> >>> #!/usr/bin/ruby
> >>> file = File.open('some_file.txt')

> >>> # Going through each line
> >>> file.each do |line|
> >>> data = line.split("\t")
> >>> total += data[1] # How do I make this line work?
> >>> end

> >>> puts total

> >> awk "{t += $2} END {print t}" some_file.txt

> > ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

> Just because you can still write your Perl in Ruby, doesn't mean that
> you *should*.

> -Rob

> Rob Biedenharn http://agileconsultingllc.com
> R...@AgileConsultingLLC.com

just ignore waxman, he's a known troll.

Well, I have a two-hexdigit IQ :wink:

Note that I don't fault your awk example, but I stand by my opinion. If the OP ever responds, we'll know who's advice has more long-term value. My initial response pointed out that the local variable, total, was scoped within the block and that the split portion of the line needed to be converted to an arithmetic value before being added to the total.

I made the assumption that chutsu wanted to learn something about Ruby and not just how to obtain a very specific result. If there's more to his/her situation, your one-liner won't make it too far.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

···

On Aug 7, 2009, at 10:05 PM, w_a_x_man wrote:

On Aug 7, 7:43 pm, Rob Biedenharn <R...@AgileConsultingLLC.com> wrote:

On Aug 7, 2009, at 8:10 PM, w_a_x_man wrote:

On Aug 7, 7:02 pm, w_a_x_man <w_a_x_...@yahoo.com> wrote:

On Aug 7, 11:44 am, chutsu <chu...@gmail.com> wrote:

I've got a file that is in two columns, how do I add the second
column
up as I read through the file?

Code:
#!/usr/bin/ruby
file = File.open('some_file.txt')

# Going through each line
file.each do |line|
data = line.split("\t")
total += data[1] # How do I make this line work?
end

puts total

awk "{t += $2} END {print t}" some_file.txt

ruby -ane"BEGIN{$t=0}; $t += $F[1].to_f; END{p $t}" some_file.txt

Just because you can still write your Perl in Ruby, doesn't mean that
you *should*.

An example from Matz (Ruby in a Nutshell):

ruby -ne 'print if /Ruby/' /usr/share/dict/words

Matz put these features in Ruby so that they could be used,
not so that they would not be used.

But in your blind arrogance, you believe that because of your
very special two-digit IQ, you are the sole arbiter of what is
permissible with Ruby. And when someone dares to post code that
does not conform to your standards of bloat, pretentiousness,
pomposity, inefficiency, and intellectual mediocrity, you lash
out with self-righteous savagery.

As a man and as a programmer, you are a sick joke.