From: Brian Candler Date: 2009-01-11T22:29:51+09:00 Subject: Re: sort hash, hash key and val off by one, see attached Mmcolli00 Mom wrote: > (my project consist of me comparing 2 files, there is more to it, but i > just wanted to explain what I am stumped on at this point.) I'm afraid your problem makes absolutely no sense at all to me. You could try explaining it more clearly, or give an example of the input together with the output you expect to get for this entire input. However I just wanted to point out to you a couple of things: > temp.each_line do |line| ... > #method to store first value > def storeKey(key,nextval = false) > storedKey = key > return storedKey if nextval == true > end Ruby doesn't support nested methods. What you're actually doing here is defining a new method *every* time this loop iterates. Better to move this outside of the loop (and then it will probably become an instance method of a class) > @@newKey = rows[key].to_s You're using a class variable here, but it's not clear why, as you're not inside any class definition; implicitly it ends up as that of the 'main' object. Much better to use a normal local variable. Even a global variable ($newKey) would be better. Also: hashes in ruby 1.8 are unordered. There is no 'next' method. If there were (or if you iterate through all the elements using 'each') you'd get them in an arbitrary order. Regards, Brian. -- Posted via http://www.ruby-forum.com/.