Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

While statements in ruby

3 views
Skip to first unread message

Mark Mr

unread,
Dec 11, 2007, 12:25:33 PM12/11/07
to
Hi guys, I have a probably simple question. I dont know how to do
iteration loops in ruby that reference more than one item of an array or
more than one array. Here's an example of what I would do in php. Can
anyone convert this to ruby? Thanks :)

while ($i=0; $i < $response_table.length; $i += 2) {

<td> $response_table[$i] </td>
<td> $response_table[$i + 1] </td>

}

or something like this would work too if i made an array for questions
and a separate one for answers

while ($i=0; $i < array_count; $i++) {

<td> $question_table[$i] </td>
<td> $answer_table[$i] </td>

}
--
Posted via http://www.ruby-forum.com/.

Phrogz

unread,
Dec 11, 2007, 12:32:03 PM12/11/07
to
On Dec 11, 10:25 am, Mark Mr <pimea.m...@gmail.com> wrote:
> Hi guys, I have a probably simple question. I dont know how to do
> iteration loops in ruby that reference more than one item of an array or
> more than one array. Here's an example of what I would do in php. Can
> anyone convert this to ruby? Thanks :)
>
> while ($i=0; $i < $response_table.length; $i += 2) {
>
> <td> $response_table[$i] </td>
> <td> $response_table[$i + 1] </td>
>
> }
>
> or something like this would work too if i made an array for questions
> and a separate one for answers

irb(main):001:0> a = %w| a b c d e f |
=> ["a", "b", "c", "d", "e", "f"]

irb(main):005:0> a.each_with_index{ |val, i|
irb(main):006:1* print val, a[i+1], "\n"
irb(main):007:1> }
ab
bc
cd
de
ef
fnil

irb(main):009:0> require 'enumerator'
irb(main):011:0> a.each_cons(2){ |x,y|
irb(main):012:1* print x, y, "\n"
irb(main):013:1> }
ab
bc
cd
de
ef

irb(main):014:0> b = %w| 1 2 3 4 5 6 |
=> ["1", "2", "3", "4", "5", "6"]

irb(main):015:0> a.zip(b)
=> [["a", "1"], ["b", "2"], ["c", "3"], ["d", "4"], ["e", "5"],
["f", "6"]]

irb(main):016:0> a.zip(b).each{ |alpha, num|
irb(main):017:1* print alpha, num, "\n"
irb(main):018:1> }
a1
b2
c3
d4
e5
f6

Phrogz

unread,
Dec 11, 2007, 12:39:21 PM12/11/07
to
On Dec 11, 10:25 am, Mark Mr <pimea.m...@gmail.com> wrote:
> while ($i=0; $i < $response_table.length; $i += 2) {
>
> <td> $response_table[$i] </td>
> <td> $response_table[$i + 1] </td>
>
> }

Oh, and some non-rubyish answers:

irb(main):019:0> i=0
irb(main):020:0> while i<a.length
irb(main):021:1> print a[i], a[i+1], "\n"
irb(main):022:1> i += 1
irb(main):023:1> end


ab
bc
cd
de
ef
fnil

irb(main):024:0> 0.step( a.length-1, 2 ){ |i|
irb(main):025:1* print a[i], a[i+1], "\n"
irb(main):026:1> }
ab
cd
ef

Chris Shea

unread,
Dec 11, 2007, 12:41:08 PM12/11/07
to
On Dec 11, 10:25 am, Mark Mr <pimea.m...@gmail.com> wrote:

If you are really just working with two arrays that map one-to-one and
onto each other, why not use one array? An array of hashes perhaps.
Like:

q_and_as = [ {:question => 'Is it blue?', :answer => 'Yes'},
{:question => 'Is it round?', :answer => 'No'} ]

Then use Array's each method:

q_and_as.each do |query|
puts "<td>#{query[:question]}</td>"
puts "<td>#{query[:answer]}</td>"
end

HTH,
Chris

Joseph Lenton

unread,
Dec 11, 2007, 12:55:01 PM12/11/07
to

To make a similar loop to yours above you can use the times method.
Like:

response_table.size.times do |i|
response_table[i]
response_table[i+1]
end

question_table.size.times do |i|
question_table[i]
answer_table[i]
end

Robert Klemme

unread,
Dec 11, 2007, 5:00:52 PM12/11/07
to
On 11.12.2007 18:25, Mark Mr wrote:
> Hi guys, I have a probably simple question. I dont know how to do
> iteration loops in ruby that reference more than one item of an array or
> more than one array. Here's an example of what I would do in php. Can
> anyone convert this to ruby? Thanks :)
>
> while ($i=0; $i < $response_table.length; $i += 2) {
>
> <td> $response_table[$i] </td>
> <td> $response_table[$i + 1] </td>
>
> }

I am amazed nobody mentioned each_slice: with "enumerator" you can do:

irb(main):006:0> (1..6).each_slice(2) {|a,b| print a,",",b,"\n"}
1,2
3,4
5,6
=> nil

> or something like this would work too if i made an array for questions
> and a separate one for answers
>
> while ($i=0; $i < array_count; $i++) {
>
> <td> $question_table[$i] </td>
> <td> $answer_table[$i] </td>
>
> }

questions.size.times do |i|
puts questions[i], answers[i]
end

Kind regards

robert

Gavin Kistner

unread,
Dec 12, 2007, 10:46:19 AM12/12/07
to
Mark Mr wrote:
> Hi guys, I have a probably simple question. I dont know how to do
> iteration loops in ruby that reference more than one item of an array or
> more than one array. Here's an example of what I would do in php. Can
> anyone convert this to ruby? Thanks :)

Many answers to your question are here:
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/a1fd3829ff41e482

Suggest you stop using ruby-forum until it stops hiding the answers
being given.

Mark Mr

unread,
Dec 12, 2007, 11:01:50 AM12/12/07
to
Hm how did my question get there? I'm a little confused :P Anyway thanks
to the first poster, that method worked great. I tried each_with_index
before i posted this and for some reason it was giving me strange
numbers instead of the corresponding values.

pimea.mark

unread,
Dec 12, 2007, 11:16:27 AM12/12/07
to

Yeah I wanted to do this as well but didn't know how in ruby :) Could
you tell me how you'd add to an array like this using variables inside
a loop? Like if you didnt manually enter the values.

Gavin Kistner

unread,
Dec 12, 2007, 11:27:56 AM12/12/07
to
Mark Mr wrote:
> Hm how did my question get there? I'm a little confused :P Anyway thanks
> to the first poster, that method worked great. I tried each_with_index
> before i posted this and for some reason it was giving me strange
> numbers instead of the corresponding values.

When you post to ruby-forum, you will see the following right above
where you type in your message:

"This forum is connected to a mailing list that is read by thousands of
people. Before you post, please use the FAQ, the Ruby documentation and
Google [...]"

ruby-talk is the main mailing list for Ruby-related discussion. A
gateway transfers information between that mailing list and the
comp.lang.ruby newsgroup.

Ruby-forum is hooked up to one or the other. (I assume the mailing
list.) For some reason, it recently has been sending messages to the
mailing list, but not posting responses as answers. So - until it's
fixed, you can sign up to the ruby-talk mailing list:
http://www2.ruby-lang.org/en/20020104.html
or you can read and post via a newsreader, or (like I do) use Google
Groups to access comp.lang.ruby:
http://groups.google.com/group/comp.lang.ruby/

Mark Mr

unread,
Dec 12, 2007, 11:48:15 AM12/12/07
to
oh ok well thanks for the tip. I tried replying via the google group
page but it didnt show up so I'll post it here instead.

In response to this reply:

If you are really just working with two arrays that map one-to-one and
onto each other, why not use one array? An array of hashes perhaps.
Like:

q_and_as = [ {:question => 'Is it blue?', :answer => 'Yes'},
{:question => 'Is it round?', :answer => 'No'} ]

Then use Array's each method:

q_and_as.each do |query|
puts "<td>#{query[:question]}</td>"
puts "<td>#{query[:answer]}</td>"
end

HTH,
Chris


I wanted to do this too but I didnt know how in Ruby. How would I add to
an array like this using variables in a loop? If I knew that I could
make it alot easier. Thanks :)

Ken Bloom

unread,
Dec 13, 2007, 10:21:21 AM12/13/07
to
On Tue, 11 Dec 2007 23:00:52 +0100, Robert Klemme wrote:

> On 11.12.2007 18:25, Mark Mr wrote:
>> Hi guys, I have a probably simple question. I dont know how to do
>> iteration loops in ruby that reference more than one item of an array
>> or more than one array. Here's an example of what I would do in php.
>> Can anyone convert this to ruby? Thanks :)
>>
>> while ($i=0; $i < $response_table.length; $i += 2) {
>>
>> <td> $response_table[$i] </td>
>> <td> $response_table[$i + 1] </td>
>>
>> }
>
> I am amazed nobody mentioned each_slice: with "enumerator" you can do:
>
> irb(main):006:0> (1..6).each_slice(2) {|a,b| print a,",",b,"\n"} 1,2
> 3,4
> 5,6
> => nil

Because someone did mention #each_cons
In reality, you're right and they (and most other people, myself
included) didn't read closely enough to see that the loop does
i+=2, not i+=1.

--Ken

--
Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

Phrogz

unread,
Dec 13, 2007, 12:02:04 PM12/13/07
to
On Dec 13, 8:21 am, Ken Bloom <kbl...@gmail.com> wrote:
> On Tue, 11 Dec 2007 23:00:52 +0100, Robert Klemme wrote:
> > I am amazed nobody mentioned each_slice: with "enumerator" you can do:
[...]

> Because someone did mention #each_cons
> In reality, you're right and they (and most other people, myself
> included) didn't read closely enough to see that the loop does
> i+=2, not i+=1.

Exactly why I didn't mention it. :) Glad Robert was there to pick up
the slack.

0 new messages