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

M

Mmcolli00 Mom

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
 
J

Jan Friedrich

Mmcolli00 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,193
Latest member
TopCryptoTaxSoftwares2024

Latest Threads

Top