[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

While statements in ruby

Mark Mr

12/11/2007 5:26:00 PM

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

12 Answers

Phrogz

12/11/2007 5:32:00 PM

0

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

12/11/2007 5:39:00 PM

0

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

12/11/2007 5:41:00 PM

0

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
>
> while ($i=0; $i < array_count; $i++) {
>
> <td> $question_table[$i] </td>
> <td> $answer_table[$i] </td>
>
> }
>
> --
> Posted viahttp://www.ruby-....

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

12/11/2007 5:55:00 PM

0

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>
>
> }
>
> 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[i]
response_table[i+1]
end

question_table.size.times do |i|
question_table[i]
answer_table[i]
end
--
Posted via http://www.ruby-....

Robert Klemme

12/11/2007 10:01:00 PM

0

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

Phrogz

12/12/2007 3:46:00 PM

0

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/a1fd38...

Suggest you stop using ruby-forum until it stops hiding the answers
being given.
--
Posted via http://www.ruby-....

Mark Mr

12/12/2007 4:02:00 PM

0

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.
--
Posted via http://www.ruby-....

Mark Mr

12/12/2007 4:16:00 PM

0

On Dec 11, 11:41 am, Chris Shea <cms...@gmail.com> wrote:
> 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
>
> > while ($i=0; $i < array_count; $i++) {
>
> > <td> $question_table[$i] </td>
> > <td> $answer_table[$i] </td>
>
> > }
>
> > --
> > Posted viahttp://www.ruby-....
>
> 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.

Phrogz

12/12/2007 4:28:00 PM

0

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/200...
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....
--
Posted via http://www.ruby-....

Mark Mr

12/12/2007 4:48:00 PM

0

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

--
Posted via http://www.ruby-....