# Question about iterators and reading a file

**URL:** <https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464>\
**Category:** Ruby\
**Created:** [December 7, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464 "2007-12-07T20:18:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [December 7, 2007, 8:18pm UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464/1 "2007-12-07T20:18:54Z")

</div>

Hi All,

I’m trying to write a loop to read in a file with the following  
structure:

... ...

And I have this piece of loop:

f = open(File, ‘r’).each do |l|  
strIn = l.chomp

```
# Now what!?!!

```

end

I need to advance to the next line of the file within the loop but I  
can’t for the life of me figure out how to do that. This can’t be hard,  
but it’s defeated me so far!

TIA

Jon

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [December 7, 2007, 9:15pm UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464/2 "2007-12-07T21:15:30Z")

</div>

On 12/7/07, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need to advance to the next line of the file within the loop but I  
> can’t for the life of me figure out how to do that. This can’t be hard,  
> but it’s defeated me so far!

That’s the cool thing about ‘each’ - you don’t have to do anything  
else. The code inside the do-end block will be called for each line,  
with l set to that line. Try adding ‘puts strln’ in place of “Now  
what”. That will print the whole file. Then replace the puts with  
whatever processing you want to do on each line.

-Adam

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [December 7, 2007, 11:09pm UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464/3 "2007-12-07T23:09:53Z")

</div>

Adam S. wrote:

> On 12/7/07, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I need to advance to the next line of the file within the loop but I  
> > can’t for the life of me figure out how to do that. This can’t be hard,  
> > but it’s defeated me so far!
> 
> That’s the cool thing about ‘each’ - you don’t have to do anything  
> else. The code inside the do-end block will be called for each line,  
> with l set to that line. Try adding ‘puts strln’ in place of “Now  
> what”. That will print the whole file. Then replace the puts with  
> whatever processing you want to do on each line.
> 
> -Adam

Hi. Thanks for responding!

I wasn’t clear about this… my problem is that I want to do something  
different depending on which line I’m looking at. Here’s some psuedo  
code:

for the whole file  
read one line with the identifying string  
read one line with the count of items  
for each item  
read one line with the data  
end  
save the whole record as an object indexed by the identifying string  
end

The problem is that this is not a line-oriented set of data. So I have  
to have some kind of nested loop that advances the line counter. Or,  
there has to be some record-oriented I/O that lets me process the file  
as a set of records. Surely there’s some cool, ruby way to do this?!??

Thanks again,

Jon

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [December 8, 2007, 12:36am UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464/4 "2007-12-08T00:36:16Z")

</div>

> That assumes that record is comma separated and can be stored as a  
> simple array. For a more complicated record, use something like  
> data \<\< MyRecord.new(f.gets.chomp)
> 
> Hope this helps,

Yay! That was it.

I do think that there should be something that takes a block and calls  
that block for each record in the file. The block would read a record  
and also advance the file pointer. Actually you could rewrite the above  
like that.

Jon

---

<div class="post-metadata">

**Author:** ![jbhruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/439d5e/32.png) [@jbhruby](https://www.ruby-forum.com/u/jbhruby)\
**Post date:** [December 7, 2007, 11:48pm UTC](https://www.ruby-forum.com/t/question-about-iterators-and-reading-a-file/122464/5 "2007-12-07T23:48:38Z")

</div>

On 12/7/07, Jon H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> different depending on which line I’m looking at. Here’s some psuedo
> 
> The problem is that this is not a line-oriented set of data. So I have  
> to have some kind of nested loop that advances the line counter. Or,  
> there has to be some record-oriented I/O that lets me process the file  
> as a set of records. Surely there’s some cool, ruby way to do this?!??

I see - i misunderstood your question.  
I don’t know about cool & rubyish, but I’d skip the each and do  
something like this:

dataset={}  
File.open(“myfile.txt”, ‘r’) do |f|  
while (!f.eof)  
name = f.gets.chomp  
data=[]  
f.gets.to\_i.times {  
data \<\< f.gets.chomp.split(“,”)  
}  
dataset[name] = data  
end  
end

p dataset

That assumes that record is comma separated and can be stored as a  
simple array. For a more complicated record, use something like  
data \<\< MyRecord.new(f.gets.chomp)

Hope this helps,  
-Adam
