[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

While loop with fixnum - not working

Mmcolli00 Mom

8/5/2008 6:39:00 PM

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
--
Posted via http://www.ruby-....

4 Answers

Frederick Cheung

8/5/2008 7:49:00 PM

0


On 5 Aug 2008, at 19:38, Mmcolli00 Mom wrote:

> 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


> While ErrorCount > 0
> ErrFieldID = get_xml_attrib_value(ie,"Error","FieldID")
> code to correct error
> End
> --
> Posted via http://www.ruby-....
>


Michael Libby

8/5/2008 8:15:00 PM

0

On Tue, Aug 5, 2008 at 1:38 PM, Mmcolli00 Mom <mmc_collins@yahoo.com> wrote:
> 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

Sebastian Hungerecker

8/6/2008 8:05:00 AM

0

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

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

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Michael Libby

8/6/2008 12:31:00 PM

0

On Wed, Aug 6, 2008 at 3:04 AM, Sebastian Hungerecker
<sepp2k@googlemail.com> wrote:
> Michael Libby wrote:
>> "while nil > 0" -- in other
>> words, the condition is always false.
>
> Actually it's always a NoMethodError as nil doesn't have a > method.

Thanks for the correction.

-Michael