# Infinite Loop Problem

• 07-17-2010, 04:54 PM
JamesShijie
Infinite Loop Problem
Hi all,

I'm new to ruby, and I'm Having trouble with my code. It's an exercise for learning ruby, and I'm plowing through it. The explanation of the exercise is this:

[CODE]
For each person in your group, you want to assign a "spy" such that no one spies on themselves or someone with the same last name.

Input: A file you will name "people.txt" of the form:
Luke Skywalker <luke@theforce.net>
Leia Skywalker <leia@therebellion.org>
John Wayne <John@Western.com>
Clark Kent <super@gmail.com>
Lois Ken <super2@gmail.com>
Pop Eye <popeye@gmail.com>
Private Eye <wise@gmail.com>
Robotic Eye <me@gmail.com>
Vicki Allan <Vicki.Allan@usu.edu>
Joe Allan <joeTheAllan@usu.edu>
Terrance Allan <Terry.All@gmail.com>
Philip Allan <PhilDoll@gmail.com>

That's the text file I have to work with. It has all those random spacings in there for a reason, so it's a bit trickier to get the right input. Here's my implementation:

Code:

```class People # Contains all information about a person   attr_accessor :fname, :lname, :email, :found   def initialize(fname, lname, email,found)     @fname = fname     @lname = lname     @email = email     @found = found   end end #========================================================================   # This method gets all the names, and puts them in a string, line by line def get_the_names()   people_array = IO.readlines("people.txt")   people_array end #======================================================================== #counts the lines in the file count = 0 File.open("people.txt", 'r') { |fh|   count += 1 while fh.gets } #======================================================================== #the raw names, gotten from get_the_names() raw_names = get_the_names() #======================================================================== #Takes the raw_names, strips them, and puts them into #an array called "stripped" stripped = Array.new(count) count.times { |index|   stripped[index] = raw_names[index].scan(/\w+/) } #======================================================================== #make the array of People objects spies = Array.new(count) for i in 0..(count-1) do   #this command re-creates the email address, because it was split into 3 parts   email = "<" + stripped[i][2] + "@" + stripped[i][3] + "." + stripped[i][4] + ">"   #0 will always be fname, and 1 will always be last name, which is why they're hard-coded   spies[i] = People.new(stripped[i][0],stripped[i][1],email,false) end #======================================================================== #perform some spying! Making sure no one spies on themselves, #or someone with their same last name r_spies = spies.sort_by{ rand } rand_spies = r_spies.dup found = 12 index = 0 while found > 0 do   for k in 0..spies.length()-1 do     for l in 0..rand_spies.length()-1 do       if spies[k].lname == rand_spies[l].lname         next       end       if spies[k].email.to_s == rand_spies[l].email.to_s         next       end       if spies[k].found == true || rand_spies[l].found == true         next       end       spies[k].found = true       rand_spies[l].found = true       puts "Person " + spies[k].fname.to_s + " " + spies[k].lname.to_s + " at " + spies[k].email.to_s + " spies on " + "Person " + rand_spies[l].fname.to_s + " " + rand_spies[l].lname.to_s + " at " + rand_spies[l].email.to_s       found = found-1     end   end end```
The problem is this: It correctly spies on 6 people, but it hangs on an infinite loop after the 6th one, and I can't figure it out. Any help would be GREATLY appreciated.