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/.
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
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
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
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
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
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.
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.
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/
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 :)
> 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/
Exactly why I didn't mention it. :) Glad Robert was there to pick up
the slack.