While statements in ruby

M

Mark Mr

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>

}
 
P

Phrogz

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
 
P

Phrogz

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, 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, a[i+1], "\n"
irb(main):026:1> }
ab
cd
ef
 
C

Chris Shea

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>

}

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
 
J

Joseph Lenton

Mark said:
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>

}

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

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

question_table.size.times do |i|
question_table
answer_table
end
 
R

Robert Klemme

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, answers
end

Kind regards

robert
 
G

Gavin Kistner

M

Mark Mr

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.
 
P

pimea.mark

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>

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

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.
 
G

Gavin Kistner

Mark said:
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/
 
M

Mark Mr

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 :)
 
K

Ken Bloom

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
 
P

Phrogz

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,274
Messages
2,571,366
Members
48,051
Latest member
noblesalt

Latest Threads

Top