CGI - table from csv

M

Milo Thurston

I am attempting to convert a script that produces csv output into
a CGI script that will produce an html table. The following code
is the best I can think of (shown here with an example of the
data):

switch_summary = ["1,2,3,4,5","6,7,8,9,10","11,12,13,14,15"]

# example output
cgi.out do
cgi.html do
cgi.head { "\n"+ cgi.title{"Title"} + "\n" } +
cgi.body {
cgi.h1 { "A pox on cgi scripting"} +
cgi.table {
switch_summary.each do |val|
cgi.tr {
val.split(",").to_a.each do |l|
cgi.td {"#{l}"}
end
} + "\n"
end
}
}
end
end

However, rather than producing a table with each element of the array
to a line, and each number to a separate cell it produces this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD>
<TITLE>Title</TITLE>
</HEAD><BODY><H1>A pox on cgi
scripting</H1><TABLE>1,2,3,4,56,7,8,9,1011,12,13,14,15</TABLE></BODY></HTML>

I'd therefore be grateful if anyone would explain why, and also point me
in the direction of anything that explains cgi scripting without
assuming prior knowledge of Perl, for example (I was no good at cgi
scripts with that language, either).
Thanks.
 
C

Carlos

I am attempting to convert a script that produces csv output into
a CGI script that will produce an html table. The following code
is the best I can think of (shown here with an example of the
data):

switch_summary = ["1,2,3,4,5","6,7,8,9,10","11,12,13,14,15"]

# example output
cgi.out do
cgi.html do
cgi.head { "\n"+ cgi.title{"Title"} + "\n" } +
cgi.body {
cgi.h1 { "A pox on cgi scripting"} +
cgi.table {
switch_summary.each do |val|
cgi.tr {
val.split(",").to_a.each do |l|
cgi.td {"#{l}"}
end
} + "\n"
end
}
}
end
end

#each returns the original array. The block is used for side-effects. To get
an array with each element modified by the block, use #collect.

cgi.table {
switch_summary.collect do |val|
cgi.tr {
val.split(",").collect do |l|
cgi.td { l }
end
} + "\n"
end
}

HTH
 
M

Milo Thurston

Carlos said:
#each returns the original array. The block is used for side-effects. To get
an array with each element modified by the block, use #collect.

Thanks, that was most useful.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top