Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3019 > unrolled thread
| Started by | Daniel Johnson <zaldivar1841@gmail.com> |
|---|---|
| First post | 2011-04-16 13:27 -0500 |
| Last post | 2011-04-16 20:04 -0500 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.ruby
For loops trouble Daniel Johnson <zaldivar1841@gmail.com> - 2011-04-16 13:27 -0500
Re: For loops trouble Josh Cheek <josh.cheek@gmail.com> - 2011-04-16 13:36 -0500
Re: For loops trouble Josh Cheek <josh.cheek@gmail.com> - 2011-04-16 13:38 -0500
Re: For loops trouble 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-16 15:10 -0500
Re: For loops trouble Christopher Dicely <cmdicely@gmail.com> - 2011-04-16 15:30 -0500
Re: For loops trouble botp <botpena@gmail.com> - 2011-04-16 20:04 -0500
| From | Daniel Johnson <zaldivar1841@gmail.com> |
|---|---|
| Date | 2011-04-16 13:27 -0500 |
| Subject | For loops trouble |
| Message-ID | <020937ce0d7f4e0fc8a6590932f71e15@ruby-forum.com> |
I am trying to translate a program I have in Java into ruby and having trouble translating this for loop for(int i = 0; a[i] < x;i++) I tried this for i in a[i]...x but it produce some errors. Any help will be appreciated. Thank you. -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-16 13:36 -0500 |
| Message-ID | <BANLkTikb7hW4kgM95hijYP=nOWTXWhqjhQ@mail.gmail.com> |
| In reply to | #3019 |
[Note: parts of this message were removed to make it a legal post.] On Sat, Apr 16, 2011 at 1:27 PM, Daniel Johnson <zaldivar1841@gmail.com>wrote: > I am trying to translate a program I have in Java into ruby and having > trouble translating this for loop > for(int i = 0; a[i] < x;i++) > I tried this > for i in a[i]...x > but it produce some errors. Any help will be appreciated. Thank you. > > -- > Posted via http://www.ruby-forum.com/. > > numbers = *1..10 max = 6 numbers.each do |num| break unless num < max puts num end
[toc] | [prev] | [next] | [standalone]
| From | Josh Cheek <josh.cheek@gmail.com> |
|---|---|
| Date | 2011-04-16 13:38 -0500 |
| Message-ID | <BANLkTimQ536N1x_jH6MvdR0osGXB_q0YBA@mail.gmail.com> |
| In reply to | #3020 |
[Note: parts of this message were removed to make it a legal post.] On Sat, Apr 16, 2011 at 1:36 PM, Josh Cheek <josh.cheek@gmail.com> wrote: > On Sat, Apr 16, 2011 at 1:27 PM, Daniel Johnson <zaldivar1841@gmail.com>wrote: > >> I am trying to translate a program I have in Java into ruby and having >> trouble translating this for loop >> for(int i = 0; a[i] < x;i++) >> I tried this >> for i in a[i]...x >> but it produce some errors. Any help will be appreciated. Thank you. >> >> -- >> Posted via http://www.ruby-forum.com/. >> >> > numbers = *1..10 > max = 6 > > numbers.each do |num| > break unless num < max > puts num > end > I guess, depending on your needs, you might want the index as well, in which case numbers.each do |num| becomes numbers.each_with_index do |num, index|
[toc] | [prev] | [next] | [standalone]
| From | 7stud -- <bbxx789_05ss@yahoo.com> |
|---|---|
| Date | 2011-04-16 15:10 -0500 |
| Message-ID | <18d4507bb264800ed91298765159d5a1@ruby-forum.com> |
| In reply to | #3019 |
Daniel Johnson wrote in post #993238: > I am trying to translate a program I have in Java into ruby and having > trouble translating this for loop > for(int i = 0; a[i] < x;i++) > I tried this > for i in a[i]...x > but it produce some errors. Any help will be appreciated. Thank you. Ruby has a for-in loop: a = [2, 4, 1, 9, 6] x = 7 for num in a break if num > x puts num end But a for-in loop calls each(), so ruby programmers just use each() directly: a.each do |num| break if num > x puts num end -- Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Christopher Dicely <cmdicely@gmail.com> |
|---|---|
| Date | 2011-04-16 15:30 -0500 |
| Message-ID | <BANLkTinT6VZHvC+zoxvDi2U8X6EMBz1Gqg@mail.gmail.com> |
| In reply to | #3019 |
On Sat, Apr 16, 2011 at 11:27 AM, Daniel Johnson <zaldivar1841@gmail.com> wrote:
> I am trying to translate a program I have in Java into ruby and having
> trouble translating this for loop
> for(int i = 0; a[i] < x;i++)
> I tried this
> for i in a[i]...x
> but it produce some errors. Any help will be appreciated. Thank you.
A simple literal translation goes from this:
for (int i=0; a[i] < x; i++) { ... }
to this Ruby:
i = 0
while a[i] < x
...
i += 1
end
That Ruby is almost certainly not the best way to translate the Java
loop, but the best way really depends on what you are doing in the
loop.
[toc] | [prev] | [next] | [standalone]
| From | botp <botpena@gmail.com> |
|---|---|
| Date | 2011-04-16 20:04 -0500 |
| Message-ID | <BANLkTi=F1WceZKNGSkbSshwwn28=RVZv1g@mail.gmail.com> |
| In reply to | #3019 |
On Sun, Apr 17, 2011 at 2:27 AM, Daniel Johnson <zaldivar1841@gmail.com> wrote:
> for(int i = 0; a[i] < x;i++)
there are many ways.
eg,
> x
=> 6
> a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
> a.take_while{|e| e<x}
=> [1, 2, 3, 4, 5]
> a.each_with_index.take_while{|e| e<x}
=> [[1, 0], [2, 1], [3, 2], [4, 3], [5, 4]]
kind regards -botp
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web