# Error in While Loop

**URL:** <https://www.ruby-forum.com/t/error-in-while-loop/161217>\
**Category:** Ruby\
**Created:** [February 24, 2009, 11:58am UTC](https://www.ruby-forum.com/t/error-in-while-loop/161217 "2009-02-24T11:58:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 24, 2009, 11:58am UTC](https://www.ruby-forum.com/t/error-in-while-loop/161217/1 "2009-02-24T11:58:19Z")

</div>

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.  
Attached is the XML file for your referance.

* * *

require ‘rexml/document’  
require ‘rio’  
include REXML

$doc = Document.new File.new(“script\_data.xml”)

$TXN = XPath.match($doc, “//TXN[@value]”)

$i = 0

def check  
@a = $TXN.size  
while $i \<= @a #|| $TXN[@i] != 0 do  
loop\_1  
@b = $TXN[$i].size  
@j=1  
while @j \<= @b || $TXN[$i][@j] != nil do  
loop\_2  
@c = $TXN[$i][@j].size  
@k=1  
while @k \<= @c || $TXN[$i][@j][@k] != nil do  
loop\_3  
@k +=2;  
end  
@j +=2; @k = 1;  
end  
$i +=1;  
end  
end

def loop\_1  
puts $TXN[$i]  
puts “AT LOOP 1”  
end

def loop\_2  
p $TXN[$i][@j]  
p “value of i #{$i}, j is #{@j}”  
puts “AT LOOP 2”  
end

def loop\_3  
puts $TXN[$i][@j][@k]  
p “value of i #{$i}, j is #{@j}, k is #{@k}”  
puts “AT LOOP 3”  
end

check

* * *

regards,  
Naresh

---

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 24, 2009, 3:10pm UTC](https://www.ruby-forum.com/t/error-in-while-loop/161217/2 "2009-02-24T15:10:05Z")

</div>

Naresh R. 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.

---

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 24, 2009, 4:54pm UTC](https://www.ruby-forum.com/t/error-in-while-loop/161217/3 "2009-02-24T16:54:42Z")

</div>

On Feb 24, 2009, at 5:57 AM, Naresh R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> while @j <= @b || $TXN[$i][@j] != nil do
> 
> ```
> 
> end  
> puts “AT LOOP 2”
> 
> regards,  
> Naresh
> 
> Attachments:  
> [http://www.ruby-forum.com/attachment/3337/script\_data.xml](http://www.ruby-forum.com/attachment/3337/script_data.xml)
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

Doesn’t File.new create a new file? Cant recall off hand…
