0

I am trying to display 2 dimension array. using below code. However for each array's first subscript I have to code one loop. Can I do it using an outer loop instead of multiple loops?

  <% tempTickets = subject['permailaddr-addrline1'] %>  
  <% cnt = tempTickets.length %>
  <% tempTickets.each_with_index do |ticket, index| %>
<div>
<tr>
  <td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-addresstype'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-addrline1'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-state'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-suburb'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-postcode'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['permailaddr-countrycode'][index] %></span></div></li></td>
</tr>
</div>
  <% end %>
  <% tempTickets = subject['perresiaddr-addrline1'] %>  
  <% cnt = tempTickets.length %>
  <% tempTickets.each_with_index do |ticket, index| %>
<div>
<tr>
  <td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-addresstype'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-addrline1'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-state'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-suburb'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-postcode'][index] %></span></div></li></td>
  <td><li class="entity_info"><div><span class="label"><%= subject['perresiaddr-countrycode'][index] %></span></div></li></td>
</tr>
</div>
  <% end %>

3 Answers 3

1
ticketLists = [ subject['permailaddr-addrline1'], subject['perresiaddr-addrline1'] ]

ticketLists.each do |tempTickets|:

And then you include the code you want to do for each tempTickets array.

Sign up to request clarification or add additional context in comments.

1 Comment

The number of loops is fine but the values are not coming up properly within the loop. any way to get the values as well?
0

Something like this should work.

<% ['permailaddr', 'perresiaddr'].each do |prefix| %>
  <% tempTickets.each_with_index do |ticket, index| %>
    <div>
    <tr>
      <td><li class="entity_info"><div><span class="label"><%= subject['memhead-mem-recno'][index] %></span></div></li></td>
      <% ['addresstype', 'addrline1', 'state', 'suburb', 'postcode', 'countrycode'].each do |field_name| %>
        <td><li class="entity_info"><div><span class="label"><%= subject["#{prefix}-#{field_name}"][index] %></span></div></li></td>
      <% end %>
    </tr>
    </div>
  <% end %>
<% end %>

Note I've also pulled the field names - ['addresstype', 'addrline1', 'state', 'suburb', 'postcode', 'countrycode'] - out into an array and looped over them, to save some duplicated html - but that's not needed if you don't want it.

1 Comment

OK, I haven't tested it. The basic concept should work - you may need to modify syntax or something.
0

Try like,

 2.times do |i|
    ...
   <%= subject['#{ i==0 ? "permailaddr" : "perresiaddr"}-addresstype'][index] %>
    ...
 end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.