# Iterating through hash to line up dates correctly in a table

**URL:** <https://www.ruby-forum.com/t/iterating-through-hash-to-line-up-dates-correctly-in-a-table/127574>\
**Category:** Rails\
**Created:** [January 30, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/iterating-through-hash-to-line-up-dates-correctly-in-a-table/127574 "2008-01-30T14:29:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [January 30, 2008, 2:29pm UTC](https://www.ruby-forum.com/t/iterating-through-hash-to-line-up-dates-correctly-in-a-table/127574/1 "2008-01-30T14:29:33Z")

</div>

I have a simple table right now:

 \<% for week in @week\_endings%\> \<%end%\> \<% @results[result].each do |test| %\> \<%end%\> \<%end%\>

| OBS Number | \<%=week.week\_ending%\> |
| \<%=result%\> | Date: \<%= test[0] %\> - Hours: \<%= test[1] %\> |

@week\_endings contains dates that are applicable to my @results object  
which is a nested hash of ID’s containing dates which have associated  
values.

After I setup my header which would look like:  
OBS Number 01-04-2008 01-11-2008

I would like to iterate through my hash and where the data applicable,  
place it in the table in the column containing the date it pertains to.

So a row would look like the following with the hash:  
{“25”,{“01-04-2008”,“5.0”},{“01-11-2008”,“10.0”}}  
{“30”,{“01-11-2008”,“10.0”}}

OBS Number 01-04-2008 01-11-2008  
25 5.0 10.0  
30 0.0 10.0

Where I’m building my rows using my hash with the following:  
\<removed\_email\_address@domain.invalid do |result| %\>

  
\<%=result%\>  
\<% @results[result].each do |test| %\>  
Date: \<%= test[0] %\> - Hours: \<%= test[1] %\>  
\<%end%\>  
  
\<%end%\>[/code]  
I would think I could use my @week\_endings object…

So something along the lines of  
for result  
for week  
if result.week == week

value  
else  
0  
end  
end

An value for a day for given ID may not exist. So if could have a value  
for the 11th but not the 4th so I need to skip by the 4th by putting a 0  
in the field and move on to the 11th and insert that value.

But when I tried to code that my values weren’t printing correctly.

Any thoughts would be appreciated, sorry if this comes off as confusing,  
I tried to make it sound as simple as possible 🙂 I can always  
elaborate if needed.

Thanks!
