Iterate through Nested Array - Noob question

D

Dina W.

I am new to ruby, I am trying to populate data into a form using a set
of variables in nested loops

For i do 1..2 do
project = @blah.create:)project, :blah =>......

The above is just a spinet, but works fine

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create:)contact, :eek:wner => owner, :status =>
status, :project = project)

If I add:
current_owner = owner[(j-1) % owner.size] it just uses the first
variable over and over again

What I am trying to accomplish:
Create two new projects
For each project I am creating 4 new forms
For each form I want to rotate through the variables for owner and
status. If I reach the end of a list of variables (only 3 for owners). I
want it to repeat through the list.



The other scenario:

For i do 1..10 do
project = @blah.create:)project, :blah =>......

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create:)contact, :eek:wner => owner, :status =>
status)

QUESTION: By having more projects than data for owners? do I have to do
anything special to ensure it continues to loop through owner and status
until i is done?
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

I am new to ruby, I am trying to populate data into a form using a set
of variables in nested loops

For i do 1..2 do
project = @blah.create:)project, :blah =>......

The above is just a spinet, but works fine

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create:)contact, :eek:wner => owner, :status =>
status, :project = project)

If I add:
current_owner = owner[(j-1) % owner.size] it just uses the first
variable over and over again
Did you also update the hash where you specify who is the owner? (something
to consider: in the above code, you pass an Array here, but now that you
have selected a desired owner, you have a String. Since I don't know what
code you are working with, I can't check for you that this is acceptable).

Also, if you iterate over 0..3, then you don't have to subtract 1 from j.
Also, I think you might be expecting that to iterate over 1,2,3 but two dots
is inclusive, so 1..4 will iterate over 1,2,3,4 I suspect you actually want
0..2 or, using three dots, which is exclusive, 0...3 either of which will
iterate over 0,1,2, and then you won't have to subtract 1 from i before you
mod it.

The other scenario:

For i do 1..10 do
project = @blah.create:)project, :blah =>......

For j in 1..4 do
owner = ["tim", "alice", "candy"]
status = ["new", "current", "out_of_date", "banned"]
contact = @contact.create:)contact, :eek:wner => owner, :status =>
status)

QUESTION: By having more projects than data for owners? do I have to do
anything special to ensure it continues to loop through owner and status
until i is done?
Right now, it is not iterating through status at all (it passes the entire
status array each time). With your above method of selecting an owner, it
does iterate through owner (though possibly incorrectly), and that is based
on the value of j, which is reset for each project. So assuming you are okay
with that, then no, you do not need to do anything special.
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top