Phrogz
12/11/2007 5:32:00 PM
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