# Using count += 1, code won't change value in output

**URL:** <https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855>\
**Category:** Ruby\
**Created:** [December 17, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855 "2008-12-17T16:30:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 17, 2008, 4:30pm UTC](https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855/1 "2008-12-17T16:30:56Z")

</div>

This is supposed to count every line in a file that contails the string  
‘0004343’ however, when I use the puts it will only output 1 and 0. My  
file contains 8 instances of ‘0004343’ so I know that this isn’t right.  
I was thinking that it never changes the value to what happens to it in  
the while loop. I know this is probably really simple. I just can’t get  
it. Please help me? MC

@count = 1  
@total = 0  
while line = f1.gets  
if f1.gets =~ /0004343/ then  
@total = @count+= 1  
end  
end  
puts @count  
puts @total

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 17, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855/2 "2008-12-17T16:35:28Z")

</div>

Mmcolli00 Mom wrote:

> This is supposed to count every line in a file that contails the string  
> ‘0004343’ however, when I use the puts it will only output 1 and 0. My  
> file contains 8 instances of ‘0004343’ so I know that this isn’t right.  
> I was thinking that it never changes the value to what happens to it in  
> the while loop. I know this is probably really simple. I just can’t get  
> it. Please help me? MC
> 
> @count = 1  
> @total = 0  
> while line = f1.gets  
> if f1.gets =~ /0004343/ then  
> @total = @count+= 1  
> end  
> end  
> puts @count  
> puts @total

Change line 4 to:  
if line =~ /0004343/ then

You read twice from the file.

Regards,  
Jan

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 17, 2008, 8:12pm UTC](https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855/3 "2008-12-17T20:12:09Z")

</div>

On Dec 17, 10:23 am, Mmcolli00 Mom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is supposed to count every line in a file that contails the string  
> ‘0004343’

f1.grep(/0004343/).size

– Mark.

---

<div class="post-metadata">

**Author:** ![mmcolli00](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@mmcolli00](https://www.ruby-forum.com/u/mmcolli00)\
**Post date:** [December 17, 2008, 4:55pm UTC](https://www.ruby-forum.com/t/using-count-1-code-wont-change-value-in-output/155855/4 "2008-12-17T16:55:57Z")

</div>

thanks
