[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

For Loop question

Mark Mr

1/29/2008 10:12:00 PM

ok basically i cant quite figure out how to do a for loop i want in
ruby. Here's what i want to do in C++, with some ruby mixed in

for (i = 0; @question_array[i].nil?; i++)
{

code here....

}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)
--
Posted via http://www.ruby-....

12 Answers

Jason Roelofs

1/29/2008 10:16:00 PM

0

Assuming you want to know where in the array said item is, this will do:

@question_array.each_with_index do |part, i|
if part.nil?
count = i
break
end
end

otherwise the whole inner if can be simplified to "break if part.nil?"

Jason

On Jan 29, 2008 5:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:
> ok basically i cant quite figure out how to do a for loop i want in
> ruby. Here's what i want to do in C++, with some ruby mixed in
>
> for (i = 0; @question_array[i].nil?; i++)
> {
>
> code here....
>
> }
>
> it's supposed to run until it finds an element of the array that isnt
> blank and then stop. anyone know how to make a loop like this in ruby?
> thanks :)
> --
> Posted via http://www.ruby-....
>
>

Jason Roelofs

1/29/2008 10:16:00 PM

0

Crap, I mean

if !part.nil?

Jason

On Jan 29, 2008 5:15 PM, Jason Roelofs <jameskilton@gmail.com> wrote:
> Assuming you want to know where in the array said item is, this will do:
>
> @question_array.each_with_index do |part, i|
> if part.nil?
> count = i
> break
> end
> end
>
> otherwise the whole inner if can be simplified to "break if part.nil?"
>
> Jason
>
>
> On Jan 29, 2008 5:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:
> > ok basically i cant quite figure out how to do a for loop i want in
> > ruby. Here's what i want to do in C++, with some ruby mixed in
> >
> > for (i = 0; @question_array[i].nil?; i++)
> > {
> >
> > code here....
> >
> > }
> >
> > it's supposed to run until it finds an element of the array that isnt
> > blank and then stop. anyone know how to make a loop like this in ruby?
> > thanks :)
> > --
> > Posted via http://www.ruby-....
> >
> >
>

Thomas Preymesser

1/29/2008 10:23:00 PM

0

On 29/01/2008, Mark Mr <pimea.mark@gmail.com> wrote:
>
> ok basically i cant quite figure out how to do a for loop i want in
> ruby. Here's what i want to do in C++, with some ruby mixed in
>
> for (i =3D 0; @question_array[i].nil?; i++)
> {
>
> code here....
>
> }
>
> it's supposed to run until it finds an element of the array that isnt
> blank and then stop. anyone know how to make a loop like this in ruby?
> thanks :)
> --
> Posted via http://www.ruby-....
>
>
i =3D 0
while @question_array[i].nil? do
code here....
i +=3D 1
end

this is the semantic aequivalent of the C/C++ loop above.


--=20
Thomas Preymesser
thopre@gmail.com
thomas@thopre.com
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.word...
http://www.t...

Dick Davies

1/29/2008 10:30:00 PM

0

question_array.each { |element|
next if element.nil
element.do_something
}

will call do_something() on every element in the array that isn't nil.

If you just want to do something with the first non-nil element, you could do

non_nil = element.find { |e| !e.nil? }
non_nil.do_something

On Jan 29, 2008 10:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:
> . Here's what i want to do in C++, with some ruby mixed in
>
> for (i = 0; @question_array[i].nil?; i++)
> {
> code here....
> }
>
> it's supposed to run until it finds an element of the array that isnt
> blank and then stop. anyone know how to make a loop like this in ruby?
> thanks :)

--
Rasputnik :: Jack of All Trades - Master of Nuns
http://number9.helloope...

7stud --

1/29/2008 10:50:00 PM

0

Mark Mr wrote:
> ok basically i cant quite figure out how to do a for loop i want in
> ruby. Here's what i want to do in C++, with some ruby mixed in
>
> for (i = 0; @question_array[i].nil?; i++)
> {
>
> code here....
>
> }
>
> it's supposed to run until it finds an element of the array that isnt
> blank and then stop. anyone know how to make a loop like this in ruby?
> thanks :)

"isnt blank" != nil
--
Posted via http://www.ruby-....

7stud --

1/29/2008 10:56:00 PM

0

7stud -- wrote:
>
> "isnt blank" != nil
>

Whoops. That should be:

"blank" != nil

--
Posted via http://www.ruby-....

7stud --

1/29/2008 11:03:00 PM

0

7stud -- wrote:
> 7stud -- wrote:
>>
>> "isnt blank" != nil
>>
>
> Whoops. That should be:
>
> "blank" != nil

Double whoops. I guess "blank" does equal nil.
--
Posted via http://www.ruby-....

Siep Korteling

1/29/2008 11:04:00 PM

0

7stud -- wrote:
> 7stud -- wrote:
>>
>> "isnt blank" != nil
>>
>
> Whoops. That should be:
>
> "blank" != nil

But what is "blank" in an array? You can't do a=[nil,,1,2,3] .
Anyway, this is also possible:

a=[nil,nil,nil,2,3,nil,4]
a.detect{|n|n}
=> 2
--
Posted via http://www.ruby-....

Jari Williamsson

1/30/2008 12:06:00 PM

0

Dick Davies wrote:
> question_array.each { |element|
> next if element.nil
> element.do_something
> }
>
> will call do_something() on every element in the array that isn't nil.

Or a version that will break after the first non-nil:

question_array.each { |element|
next if element.nil
break element.do_something
}


> If you just want to do something with the first non-nil element, you could do
>
> non_nil = element.find { |e| !e.nil? }
> non_nil.do_something

To protect from arrays with all-nils:

non_nil = element.find { |e| !e.nil? }
non_nil.do_something if non_nil


Best regards,

Jari Williamsson

ThoML

1/30/2008 2:00:00 PM

0

> it's supposed to run until it finds an element of the array that isnt
> blank and then stop.

And then do what? Do you need the index? In this case you can use the
index method.

a = [1,2,nil,3]

a.index(nil)
# => 2

or do you want the elements before that? Or ...

Anyway, another solution close in resemblance to yours could be:

for e in a
break if e.nil?
do_something e
end

HTH,
Thomas.