FOR Loop with 2 variables

Hi Everyone, Can anyone pls tell me how to use 2 variables (objects) in FOR Loop? Normally in most programming languages it will be like, for(i=1,j=1;i<=5;i++,j++) Whats the equivalent for above statement in Rails? i have two objects with same number of fields but different values. @original_object & @modified_object

Thanks in Advance,

Regards, VASANTH

Hi --

Hi Everyone, Can anyone pls tell me how to use 2 variables (objects) in FOR Loop? Normally in most programming languages it will be like, for(i=1,j=1;i<=5;i++,j++) Whats the equivalent for above statement in Rails? i have two objects with same number of fields but different values. @original_object & @modified_object

There are several possibilities, depending on exactly what you want to do (which I'm not totally clear on). One would be:

   fields.size.times do |i|      j = i      ...    end

though I suspect that if you really just need an index, you wouldn't need two variables.

You could also look into Array#zip. Beyond that I'd need to know more about what you're trying to do and how your data are organized.

David

Vasanthakumar Csk wrote:

for(i=1,j=1;i<=5;i++,j++) Whats the equivalent for above statement in Rails?

Thanks in Advance,

Regards, VASANTH

There are many types of loops. For the one in your example you could say:

until 1 <= 5 do ..... ..... end

or

1.upto(5) do |x| ........ ........ end

To use two variables you are going to have to use two loops - there is no way that I know of to write a single loops that counts double.

It also depends on what you are counting? If you have a list of stuff then the loop could be written as:

stuff.each do |x| ......... ......... end

What are you trying to do with the @modified and @oringinal objects? Are you trying to compare their fields?

Hi David and Nantz, this is what i am trying to do @window_data_original = Window.getGeneralData(building_id) @window_data_modified = ScopeOfWorkAction.getRetrofittedData(building_id,retrofit_cost_data.module_id,Window.column_names,window_data_original) So after the above two statments, i will have two different objects with same fields but different values. Now, these are multi-dimensional array objects (since a building can have any number of windows). My project is a scientific project, where i need to calculate some parameter's like below (just giving a sample):

for window_data_original in @window_data_original    calculation_value1 = window_data_original.size - window_data_modified.size    total = total + calculation_value1 end

Hope, i am clear this time. Please help me. Thanks in advance.

Regards, VASANTH

Hi --

Hi David and Nantz, this is what i am trying to do @window_data_original = Window.getGeneralData(building_id) @window_data_modified = ScopeOfWorkAction.getRetrofittedData(building_id,retrofit_cost_data.module_id,Window.column_names,window_data_original) So after the above two statments, i will have two different objects with same fields but different values. Now, these are multi-dimensional array objects (since a building can have any number of windows). My project is a scientific project, where i need to calculate some parameter's like below (just giving a sample):

for window_data_original in @window_data_original

That's some weird loop variable naming :slight_smile:

  calculation_value1 = window_data_original.size - window_data_modified.size   total = total + calculation_value1 end

If it's two arrays them you can zip them, or just use an index and fish things out:

   @w_d_orig.each_with_index do |wdo,i|      wdm = @w_d_modified[i]      # etc.    end

(if I'm understanding correctly).

David

Hi, Hope this would be clear,

for employee in @employees    calculation_value1 = employee.size - employee.size    # first is original value (employee.size) and second is modified value (employee.size)    total = total + calculation_value1 end

Here, @employees is an two-dimensional array of 50 employees & so the for loop will run for 50 times. Now, i want to iterate through both arrays simultaneously so that i will get the final result. eg:- (consider 5th employee) calculation_value1 = original_employee[5].size - modified_employee[5].size

Hope you understand my problem and help me out of this.

Thanks in advance.

Regards, Vasanth

CSK Vasanth wrote:

for employee in @employees    calculation_value1 = employee.size - employee.size    # first is original value (employee.size) and second is modified value (employee.size)    total = total + calculation_value1 end

Combining that with:

eg:- (consider 5th employee) calculation_value1 = original_employee[5].size - modified_employee[5].size

And the examples everyone has provided, you have choices like:

@orig_employees.each_with_index |orig_employee, i|   mod_employee = @mod_employees[i]   calc_val1 = orig_employee.size - mod_employee.size   total = total + calc_val1 end

or:

(0...@orig_employees.length).each |i|   orig_employee = @orig_employees[i]   mod_employee = @mod_employees[i]   calc_val1 = orig_employee.size - mod_employee.size   total = total + calc_val1 end # (yes, that was 3 dots in the range, not 2)

or:

@orig_employees.length.times |i|   orig_employee = @orig_employees[i]   mod_employee = @mod_employees[i]   calc_val1 = orig_employee.size - mod_employee.size   total = total + calc_val1 end

There are many ways you can do this using the solutions provided. Note that you *don't* need 2 indexes, since you say both arrays will be the same length. One index is enough to refer to both arrays.