[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Confusion with while loop

Michael W. Ryder

7/31/2007 12:39:00 AM

I am trying to create a method that would allow one to skip x number of
a character then change the next y number of a character. While trying
to create a simple routine I ran into two "weird" problems. The code
follows:

a = "1 2 1 3 1 4 1 5 1 6 1 7"
ns = 2, nc = 2

i = 0, il = a.length, xs = 0, xc = 0
while xs < ns
if a[i, 1] == "1"
xs += 1
end
i += 1
end

puts a[i, -1]

In this incarnation trying to run the code returns the following error:
testing.rb:5:in `<': comparison of Fixnum with Array failed
(ArgumentError). If I change ns in the while statement to 2 I get the
following error: testing.rb:6:in `[]': can't convert Array into Integer
(TypeError).

I can't see why statement 5 (the while statement) thinks that ns is an
array when it is clearly defined as an integer. I also can't figure out
why I get the error message in the following line when I have used that
code in other places with no problems. I must be missing something but
I can't figure out what it is.
4 Answers

Alex Young

7/31/2007 12:46:00 AM

0

Michael W. Ryder wrote:
> I am trying to create a method that would allow one to skip x number of
> a character then change the next y number of a character. While trying
> to create a simple routine I ran into two "weird" problems. The code
> follows:
>
> a = "1 2 1 3 1 4 1 5 1 6 1 7"
> ns = 2, nc = 2
>
> i = 0, il = a.length, xs = 0, xc = 0
> while xs < ns
> if a[i, 1] == "1"
> xs += 1
> end
> i += 1
> end
>
> puts a[i, -1]
>
> In this incarnation trying to run the code returns the following error:
> testing.rb:5:in `<': comparison of Fixnum with Array failed
> (ArgumentError). If I change ns in the while statement to 2 I get the
> following error: testing.rb:6:in `[]': can't convert Array into Integer
> (TypeError).
>
> I can't see why statement 5 (the while statement) thinks that ns is an
> array when it is clearly defined as an integer.
It's not, though - run just this line, and check the values of ns and nc:

ns = 2, nc = 2

I think you want:

ns, nc = [2,2]

--
Alex


Michael W. Ryder

7/31/2007 1:00:00 AM

0

Alex Young wrote:
> Michael W. Ryder wrote:
>> I am trying to create a method that would allow one to skip x number
>> of a character then change the next y number of a character. While
>> trying to create a simple routine I ran into two "weird" problems.
>> The code follows:
>>
>> a = "1 2 1 3 1 4 1 5 1 6 1 7"
>> ns = 2, nc = 2
>>
>> i = 0, il = a.length, xs = 0, xc = 0
>> while xs < ns
>> if a[i, 1] == "1"
>> xs += 1
>> end
>> i += 1
>> end
>>
>> puts a[i, -1]
>>
>> In this incarnation trying to run the code returns the following
>> error: testing.rb:5:in `<': comparison of Fixnum with Array failed
>> (ArgumentError). If I change ns in the while statement to 2 I get the
>> following error: testing.rb:6:in `[]': can't convert Array into
>> Integer (TypeError).
>>
>> I can't see why statement 5 (the while statement) thinks that ns is an
>> array when it is clearly defined as an integer.
> It's not, though - run just this line, and check the values of ns and nc:
>
> ns = 2, nc = 2
>
> I think you want:
>
> ns, nc = [2,2]
>

Or ns =2; nc =2. I knew it had to be something simple. Thats the
problem with programming in multiple languages at the same time, they
each do the same things slightly differently. Thanks for pointing that out.

Robert Klemme

7/31/2007 8:21:00 AM

0

2007/7/31, Michael W. Ryder <_mwryder@worldnet.att.net>:
> Alex Young wrote:
> > Michael W. Ryder wrote:
> >> I am trying to create a method that would allow one to skip x number
> >> of a character then change the next y number of a character. While
> >> trying to create a simple routine I ran into two "weird" problems.
> >> The code follows:
> >>
> >> a = "1 2 1 3 1 4 1 5 1 6 1 7"
> >> ns = 2, nc = 2
> >>
> >> i = 0, il = a.length, xs = 0, xc = 0
> >> while xs < ns
> >> if a[i, 1] == "1"
> >> xs += 1
> >> end
> >> i += 1
> >> end
> >>
> >> puts a[i, -1]
> >>
> >> In this incarnation trying to run the code returns the following
> >> error: testing.rb:5:in `<': comparison of Fixnum with Array failed
> >> (ArgumentError). If I change ns in the while statement to 2 I get the
> >> following error: testing.rb:6:in `[]': can't convert Array into
> >> Integer (TypeError).
> >>
> >> I can't see why statement 5 (the while statement) thinks that ns is an
> >> array when it is clearly defined as an integer.
> > It's not, though - run just this line, and check the values of ns and nc:
> >
> > ns = 2, nc = 2
> >
> > I think you want:
> >
> > ns, nc = [2,2]
> >
>
> Or ns =2; nc =2. I knew it had to be something simple. Thats the
> problem with programming in multiple languages at the same time, they
> each do the same things slightly differently. Thanks for pointing that out.

These are options, too:

ns, nc = 2, 2
ns = nc = 2

Kind regards

robert

Michael W. Ryder

7/31/2007 8:30:00 AM

0

Robert Klemme wrote:
> 2007/7/31, Michael W. Ryder <_mwryder@worldnet.att.net>:
>> Alex Young wrote:
>>> Michael W. Ryder wrote:
>>>> I am trying to create a method that would allow one to skip x number
>>>> of a character then change the next y number of a character. While
>>>> trying to create a simple routine I ran into two "weird" problems.
>>>> The code follows:
>>>>
>>>> a = "1 2 1 3 1 4 1 5 1 6 1 7"
>>>> ns = 2, nc = 2
>>>>
>>>> i = 0, il = a.length, xs = 0, xc = 0
>>>> while xs < ns
>>>> if a[i, 1] == "1"
>>>> xs += 1
>>>> end
>>>> i += 1
>>>> end
>>>>
>>>> puts a[i, -1]
>>>>
>>>> In this incarnation trying to run the code returns the following
>>>> error: testing.rb:5:in `<': comparison of Fixnum with Array failed
>>>> (ArgumentError). If I change ns in the while statement to 2 I get the
>>>> following error: testing.rb:6:in `[]': can't convert Array into
>>>> Integer (TypeError).
>>>>
>>>> I can't see why statement 5 (the while statement) thinks that ns is an
>>>> array when it is clearly defined as an integer.
>>> It's not, though - run just this line, and check the values of ns and nc:
>>>
>>> ns = 2, nc = 2
>>>
>>> I think you want:
>>>
>>> ns, nc = [2,2]
>>>
>> Or ns =2; nc =2. I knew it had to be something simple. Thats the
>> problem with programming in multiple languages at the same time, they
>> each do the same things slightly differently. Thanks for pointing that out.
>
> These are options, too:
>
> ns, nc = 2, 2

Maybe this is where I got confused. I remember seeing commas between
variables, just forgot the way they had to be entered. Thanks for
pointing this out.


> ns = nc = 2
>
> Kind regards
>
> robert
>