DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   Ruby (http://www.daniweb.com/forums/forum73.html)
-   -   Problem with Trie.. (http://www.daniweb.com/forums/thread183456.html)

vesper967 Mar 25th, 2009 1:20 pm
Problem with Trie..
 
So I'm writing a program that reads in a file which has each word on a different line. The program is supposed to loop through the file in such a way that when it reads a line and gets to the end of the line it adds that word to an array and inserts it into a trie that I have created then it does it for the second, third and so on. However, every time I insert a new word the existing trie is overwritten. How can I loop through so that my trie continues to hold on to the previously inserted words? And yes I know that my code is probably written horribly.

def hw2
  puts "What file would you like to read?"
  name = gets.chomp
  puts "What file would you like to write to?"
  name2 = gets.chomp

  f1 = File.new(name2, "w+")
  aFile = File.open(name, "r")
  array = []
  while char = aFile.getc
    char = char.each_byte { |ch| }
    array.push char
    first = array.first
    rest = array.reverse
    rest.pop
    rest = rest.reverse
    t2 = insert(first, rest)
   
    if char == "\n"
    array.clear
    end
  end

    print t2
    print "\n"
end


All times are GMT -4. The time now is 10:14 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC