# Substituting from another file

**URL:** <https://www.ruby-forum.com/t/substituting-from-another-file/57619>\
**Category:** Ruby\
**Created:** [April 12, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/substituting-from-another-file/57619 "2006-04-12T15:31:31Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [April 12, 2006, 3:31pm UTC](https://www.ruby-forum.com/t/substituting-from-another-file/57619/1 "2006-04-12T15:31:31Z")

</div>

I have a textfile with a long text, and a wordlist with words both in  
letters and phonetic notation. I want to substitute all the words in the  
text with their phonetic spelling from the wordlist.

The wordlist entries look like this:  
PANELED P AE1 N AH0 L D  
PANELING P AE1 N AH0 L IH0 NG  
etc.

What I have come up with so far:

f = File.open(“textfile”,“r+”)  
d = File.open(“dictionary”)  
g = File.open(“outputfile”,“r+”)  
s = IO.read(“textfile”)  
w = IO.read(“dictionary”)  
s.gsub!(/.+? /) {|match| w.match($1)}  
g.write(s)  
f.close  
d.close

This seems to enter an infinite loop.
