# Build 3x3 table from one array

**URL:** <https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057>\
**Category:** Ruby\
**Created:** [March 16, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057 "2006-03-16T18:29:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 6:29pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/1 "2006-03-16T18:29:21Z")

</div>

I am new to ruby and still haven’t got my mind around iterators although  
I think they are really cool.

I am stuck trying to figure out how to take an array of data and  
dynamically build a 3x3 html table from it without standard for loops.

Here is the code in JavaScript:

var MAX\_ROW = 3;  
var MAX\_COL = 3;  
var nIdx = 0;

print("

");  
for (var r = 0; nIdx \< aList.length && r \< MAX\_ROW; r++)  
{  
print("");  
for (var c = 0; nIdx \< aList.length && c \< MAX\_COL; c++)  
{  
print("

| ");

```
	print(aList[nIdx].name);
	nIdx++;

	print("</td>");
}	// cols
print("</tr>");

```

} // rows  
print("

 |

");

Can someone point me in the right direction for how to do this. I found  
each\_index but still was at a loss.

Thanks for the help.  
forest

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/2 "2006-03-16T18:53:46Z")

</div>

forest wrote:

> I am stuck trying to figure out how to take an array of data and  
> dynamically build a 3x3 html table from it without standard for loops.
> 
> Here is the code in JavaScript:  
> _snip_  
> Can someone point me in the right direction for how to do this. I found  
> each\_index but still was at a loss.

Without giving it a lot of thought, I’d do something to the effect of:

rows = [  
[‘col1’, ‘col2’, ‘col3’],  
[‘cOl1’, ‘Col2’, ‘col3’],  
[‘col1’, ‘COL2’, ‘COl3’],  
]

output = ‘’

output \<\< ‘

’  
rows.each do |row|  
output \<\< ‘’  
row.each do |col|  
output \<\< “”  
end  
output \<\< ‘’  
end  
output \<\< ‘

| #{col} |

’

puts output

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 7:04pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/3 "2006-03-16T19:04:51Z")

</div>

On Mar 16, 2006, at 11:29 AM, forest wrote:

> I am stuck trying to figure out how to take an array of data and  
> dynamically build a 3x3 html table from it without standard for loops.

Here’s one idea:

> > data = (1…9).to\_a  
> > =\> [1, 2, 3, 4, 5, 6, 7, 8, 9]
> 
> > require “enumerator”  
> > =\> true
> 
> > puts “
> > 
> > ”
> > 
> > =\> nil \>\> data.each\_slice(3) do |row| ?\> puts " " \>\> row.each { |cell| puts " " } \>\> puts " " \>\> end =\> nil \>\> puts "
> > 
> > | #{cell} |
> > | 1 | 2 | 3 |
> > | 4 | 5 | 6 |
> > | 7 | 8 | 9 |
> > 
> > " =\> nil
> > 
> > Hope that helps.
> > 
> > James Edward G. II

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/4 "2006-03-16T20:15:36Z")

</div>

Pistos C. wrote:

> forest wrote:
> 
> > I am stuck trying to figure out how to take an array of data and  
> > dynamically build a 3x3 html table from it without standard for loops.
> > 
> > Here is the code in JavaScript:  
> > _snip_  
> > Can someone point me in the right direction for how to do this. I found  
> > each\_index but still was at a loss.

puts %w(1 2 3 4 5 6 7 8 9).map{|x| “

” + x + “” }.  
join.gsub(/(\<td.\*?/td\>){3}/,"\n \&\n\n")

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 8:46pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/5 "2006-03-16T20:46:05Z")

</div>

Thanks all. The each\_slice solution I think will work the best for my  
scenario. I really appreciate the help.

forest

> > > data = (1…9).to\_a  
> > > =\> [1, 2, 3, 4, 5, 6, 7, 8, 9]
> > 
> > > require “enumerator”  
> > > =\> true
> > 
> > > puts “
> > > 
> > > ”
> > > 
> > > =\> nil \>\> data.each\_slice(3) do |row| ?\> puts " " \>\> row.each { |cell| puts " " } \>\> puts " " \>\> end
> > > 
> > > > Hope that helps.
> > > > 
> > > > James Edward G. II
> > > 
> > > | #{cell} |

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 10:21pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/6 "2006-03-16T22:21:59Z")

</div>

Yes, enumerator is part of the standard library. Go to http://ruby-  
[doc.org/stdlib/](http://doc.org/stdlib/) and find enumerator (right after English) in the left  
frame if ri isn’t helping.

Tim

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [January 28, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/7 "2008-01-28T12:29:18Z")

</div>

I wrote next function (maybe need to be rewritten to method for Array  
cass) -  
def array\_to\_table(arr, width)  
data = “

”  
arr.each\_slice(width) do |row|  
data \<\< “”  
row.each do |elem|  
data \<\< ("”)  
end  
data \<\< “”  
end  
data \<\< “

| " + yield(elem) + “ |

”  
end

Using -  
\<%= array\_to\_table(User.find(:all), 3) {|user| “

#{user.name}
”} %\>

---

<div class="post-metadata">

**Author:** ![forest](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f1d935/32.png) [@forest](https://www.ruby-forum.com/u/forest)\
**Post date:** [March 16, 2006, 10:12pm UTC](https://www.ruby-forum.com/t/build-3x3-table-from-one-array/55057/8 "2006-03-16T22:12:44Z")

</div>

> > > require “enumerator”

Is this standard library?

I tried ‘ri Enumerator’ to learn more, but it just comes back talking  
about SyncEnumerator, which is something else. (Trying the lowercase  
‘enumerator’ didn’t work at all.)
