[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: build 3x3 table from one array

Pistos Christou

3/16/2006 5:54:00 PM

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 << '<table>'
rows.each do |row|
output << '<tr>'
row.each do |col|
output << "<td>#{col}</td>"
end
output << '</tr>'
end
output << '</table>'

puts output

--
Posted via http://www.ruby-....


1 Answer

William James

3/16/2006 7:12:00 PM

0

Pistos Christou 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| "<td>" + x + "</td>" }.
join.gsub(/(<td.*?\/td>){3}/,"<tr>\n \\&\n</tr>\n")