While loop with fixnum - not working

M

Mmcolli00 Mom

I can't get the while loop to work. When I set ErrorCount.Class it
outputs Fixnum. Do you think that this is the reason that that the while
will not execute? Thanks in advance. Mom Mcolli00

it ("Search for FieldId Tag containing error in XML file") do
ErrFieldID= get_xml_attrib_value(ie, "Error","FieldID")

ErrorCount = ErrFieldID.size #this lets me know what size the array
is
puts ErrorCount.class #this shows that it is a Fixnum
ErrorCount.to_int #this does nothing
puts "Total Claim Errors"
puts ErrorCount #shows correct count!
end

#this while structure will not work

While ErrorCount > 0
ErrFieldID = get_xml_attrib_value(ie,"Error","FieldID")
code to correct error
End
 
F

Frederick Cheung

I can't get the while loop to work. When I set ErrorCount.Class it
outputs Fixnum. Do you think that this is the reason that that the
while

Fixnum is a subclass of Integer. For performance reasons, small
integers will be an instance of Fixnum. This is normal. (not terribly
relevant here, but it doesn't really mater what ErrorCount is as long
as it has a < method which will accept 0 as an argument)

will not execute? Thanks in advance. Mom Mcolli00

it ("Search for FieldId Tag containing error in XML file") do
ErrFieldID= get_xml_attrib_value(ie, "Error","FieldID")

ErrorCount = ErrFieldID.size #this lets me know what size the
array
is
puts ErrorCount.class #this shows that it is a Fixnum
ErrorCount.to_int #this does nothing
puts "Total Claim Errors"
puts ErrorCount #shows correct count!
end

#this while structure will not work
In what way is it not working ? (assuming the problem is not just that
while and end should be lowercase)
Also in ruby a leading capital letter indicates a constant, so you'll
probably get errors about "already initialized constant Foo" with code
like this

Fred
 
M

Michael Libby

it ("Search for FieldId Tag containing error in XML file") do
ErrFieldID= get_xml_attrib_value(ie, "Error","FieldID")

ErrorCount = ErrFieldID.size #this lets me know what size the array
is
puts ErrorCount.class #this shows that it is a Fixnum
ErrorCount.to_int #this does nothing
puts "Total Claim Errors"
puts ErrorCount #shows correct count!
end

#this while structure will not work

While ErrorCount > 0
ErrFieldID = get_xml_attrib_value(ie,"Error","FieldID")
code to correct error
End

Not sure what you mean by "will not work". If you state your
expectation and the result you actually got it will help identify what
went wrong.

If all of the code in your message is one long script, then the
ErrorCount variable you declared inside the first block (it... do..
end) has gone out of scope when the while statement is executed. Your
"while ErrorCount > 0" is really saying "while nil > 0" -- in other
words, the condition is always false.

If you have an array (like ErrFieldID) and you want to handle each
element in it, you probably want to iterate over the array like so:

err_field_id = get_xml_attrib_value(ie, "Error", "FieldID")
puts "Total claim errors #{err_field_id.size}"
err_field_id.each do |efid|
puts "Correcting error: #{efid}"
code_to_correct_error(efid)
end

-Michael
 
S

Sebastian Hungerecker

Michael said:
"while nil > 0" -- in other
words, the condition is always false.

Actually it's always a NoMethodError as nil doesn't have a > method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top