# Iterating through a file, sticking iterated array entries in

**URL:** <https://www.ruby-forum.com/t/iterating-through-a-file-sticking-iterated-array-entries-in/115108>\
**Category:** Ruby\
**Created:** [September 20, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/iterating-through-a-file-sticking-iterated-array-entries-in/115108 "2007-09-20T17:41:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 20, 2007, 5:41pm UTC](https://www.ruby-forum.com/t/iterating-through-a-file-sticking-iterated-array-entries-in/115108/1 "2007-09-20T17:41:19Z")

</div>

I’ve got an array, a listing of “titles” in an xml file. Basically, I  
want to iterate through my file and, wherever I see the tag  
, I want to replace that with \<subhead.4\>code\</subhead.4\>,  
where “code” is the next entry in the stack of my array. Now, I’ve tried  
this a number of ways and it does it, but, it doesn’t cycle through my  
array entries. It populates all of my substitutions with just the first  
entry in the array, none of the subsequent ones.  
…  
$codes = []  
$codes = xmlfile.scan(/\s\*\<issue +code _="[A-Z]{3}  
 "\>(.?)\</issue\>\s_?/mi)  
…  
$codes.each do |code|  
puts code  
xmlfile.gsub!(/?/,  
“\<subhead.4\>#{code}\</subhead.4\>/n

”)  
end

I’m getting the following. My “puts” statement, which is just there for  
checking, does indeed list each entry of the array, though.

\<subhead.4\>Trade (Domestic & Foreign)\</subhead.4\>/n

  
which is correct, but, I’m getting it for all 115 occurrences of the  
 tag.

Maybe gsub isn’t the way to do this. Obviously, I’m trying to iterate  
through every instance of and simply replace it with the  
above code. Is gsub not an iteration tool?

Thanks a lot,  
Peter

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 2:43am UTC](https://www.ruby-forum.com/t/iterating-through-a-file-sticking-iterated-array-entries-in/115108/2 "2007-09-21T02:43:14Z")

</div>

On Sep 20, 2007, at 11:41 AM, Peter B. wrote:

> …  
> I’m getting the following. My “puts” statement, which is just there  
> above code. Is gsub not an iteration tool?  
> The gsub! matches all the substitution targets on the first iteration  
> of the ‘each’; therefore, no matches occur on any subsequent  
> iterations. Try something like:

`
source = <<TEXT
The quick brown
 jumped over
the lazy 
The quick brown
 jumped over
the lazy 
TEXT
codes = %w[a b c d e f g]
source.gsub!(/<(\w*)>/) { c = codes.shift; “<#{c}>#{$1}</#{c}>”}
puts source
` The quick brown fox jumped over the lazy **dog** The quick brown fox jumped over the lazy dog

Regards, Morton

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [September 21, 2007, 3:55pm UTC](https://www.ruby-forum.com/t/iterating-through-a-file-sticking-iterated-array-entries-in/115108/3 "2007-09-21T15:55:33Z")

</div>

Morton G. wrote:

> On Sep 20, 2007, at 11:41 AM, Peter B. wrote:
> 
> > …  
> > I’m getting the following. My “puts” statement, which is just there  
> > above code. Is gsub not an iteration tool?  
> > The gsub! matches all the substitution targets on the first iteration  
> > of the ‘each’; therefore, no matches occur on any subsequent  
> > iterations. Try something like:
> 
> `
> source = <<TEXT
> The quick brown
> jumped over
> the lazy 
> The quick brown
> jumped over
> the lazy 
> TEXT
> codes = %w[a b c d e f g]
> source.gsub!(/<(\w*)>/) { c = codes.shift; “<#{c}>#{$1}</#{c}>”}
> puts source
> ` The quick brown fox jumped over the lazy **dog** The quick brown fox jumped over the lazy dog
> 
> Regards, Morton

Thanks a lot, Morton. Yes, I’ve modified my approach. I just had to  
expand my mind to really consider how to contain each discrete thing  
that I want in these huge text files I’m getting. Once I’ve got them all  
in an array, I see now that I can do stuff to each entry in the array.  
Exactly what I need. Your suggestion helped. Thanks again.
