From: Brian Candler Date: 2009-02-24T23:09:25+09:00 Subject: Re: Error in While Loop Naresh Ramaswamy wrote: > Following is my Ruby Code Wherein I am reading an XML file. > I am afraid why my loop is not getting incremented for value of i. STDERR.puts "About to increment $i:" > $i +=1; STDERR.puts "Value is now #{$i}" I think you're going about debugging in a reasonable way: just keep putting more debugging lines in until you find the problem. Don't be afraid to put your debugging code straight inline. I tend to put it lined up with the left-hand column, so it's easy to spot and rip out later. Incidentally, you have a strange mixture of global variables ($i) and instance variables (@a). Normally you would just use local variables, and pass in any parameters as required, like this: def check(txn) i = 0 a = tnx.size while i <= a #|| txn[i] != 0 .. etc end end doc = Document.new File.new("script_data.xml") txn = XPath.match(doc, "//TXN[@value]") check(txn) OR: you would make a class, and keep only those values you wish to persist in instance variables. e.g. class TXN def initialize(txn) @txn = txn end def check i = 0 a = @txn.size while i < a ... etc end end end doc = Document.new File.new("script_data.xml") txn = TXN.new XPath.match(doc, "//TXN[@value]") txn.check This latter case is useful when you want to add more methods into your TXN class, which operate on the same data, or you wish to bundle together the txn with additional objects or data, and pass them around as a single entity. -- Posted via http://www.ruby-forum.com/.