For Each Loop / Custom Iterating Variable? Is It Possible?

For Each Loop / Custom Iterating Variable? Is It Possible?

As a programmer, a client of mine asked for some help with their Zoho application. After jumping into Zoho for the last couple of hours - and seeing this code called Deluge, that is pretty self explanatory but frustrating. There is one thing I can not figure out thus far - how can I set a counter variable, and iterate through the fields of a form using "one" loop. Below, is an example where they have 40 part names, 160 lines of code - and the for each field in the code looks like the following

for each fer_product_builder in Product_Builder  [part_name_01 == input.ID]
{
    fer_product_builder.part1cost = input.part_total_cost;
}
for each fer_product_builder in Product_Builder  [part_name_02 == input.ID]
{
    fer_product_builder.part2cost = input.part_total_cost;
}

all the way to part 40, and beyond in the future.
---------------=
How do I turn their consistant 160 lines of code, into a real loop? something much easier like the following:

$i = 1;

for each
fer_product_builder in Product_Builder [part_name_{$i} == input.ID]
      fer_product_builder.part{$i}cost = input.part_total_cost;
}
----------------=

So much repetition, can this be simplified? More code, that is constantly repeated looks like the following. Again, a foreach, while loop could resolve this with setting a variable. And the reason it starts at 11 and ends at 20 is because Deluge only allows 10 from what I've been told. Again, a foreach could resolve this - yes?
----------------=
(input.comp_11_unit_weight  *  input.component_11_quantity  +  input.comp_12_unit_weight  *  input.component_12_quantity  +  input.comp_13_unit_weight  *  input.component_13_quantity  +  input.comp_14_unit_weight  *  input.component_14_quantity  +  input.comp_15_unit_weight  *  input.component_15_quantity  +  input.comp_16_unit_weight  *  input.component_16_quantity  +  input.comp_17_unit_weight  *  input.component_17_quantity  +  input.comp_18_unit_weight  *  input.component_18_quantity  +  input.comp_19_unit_weight  *  input.component_19_quantity  +  input.comp_20_unit_weight  *  input.component_20_quantity)

^^
That would be turned into this:

add = "";
for($i=1;$i <= count(items); $i++){
  add = add+(input.comp_{$i}_unit_weight  *  input.component_{$i}_quantity)
}

Thank you in advance.