我一直忙于将列表报告转换为表格数据的工作,由于各种原因,我所拥有的唯一编程工具是groovy(即没有shell命令,没有C,没有perl)。数据预先汇总,分为五列,
分组(例如公司),子分组(例如季度),小计,小组总数,总计(将摘要数据添加到每组的第一行):
Big Co.\t2009 Q4\t29\t88\t308
\t2010 Q1\t38\t\t
\t2010 Q4\t21\t\t
Gargantua Inc.\t2009 Q4\t33\t139
\t2010 Q1\t31\t\t
\t2010 Q2\t36\t\t
\t2010 Q3\t39\t\t
Mediocre Ltd.\t2009 Q4\t39\t81
\t2010 Q4\t42\t\t
输出必须是:
<table>
<tr><th>Group</th><th>2009 Q4</th><th>2010 Q1</th><th>2010 Q2</th><th>2010 Q3</th><th>2010 Q4</th><th>Subtotal</th></tr>
<tr><th>Big Co</th><td>29</td><td>38</td><td> </td><td> </td><td>38</td><th>308</th></tr>
<tr><th>Gargantua Inc</th><td>29</td><td>31</td><td>36</td><td>39</td><td> </td><th>139</th></tr>
<tr><th>Gargantua Inc</th><td>39</td><td> </td><td> </td><td> </td><td>42</td><th>81</th></tr>
<tr><th> </th><td> </td><td> </td><td> </td><td> </td><td> </td><th>308</th></tr>
</table>
我希望能够在数据上运行一些循环;将东西粘贴到一个关联数组的关联数组中,随时建立一个完整的密钥列表;然后再次循环以产生输出。我看了一下groovy,Map语法完全抛出了。
有人能指出一些更清晰的groovy Map迭代的例子,这样我至少可以在水中找到一个脚趾吗?
感激不尽的任何帮助。
(如果有人认为这只是一次钓鱼之旅,我已经将PHP版本整合在一起了 - 基本上我需要将其翻译成groovy ......)
<?php
$rows = explode("\n", $data);
$group = "";
$subgroup = "";
$table = array();
$totals = array();
$subgroup_names = array();
$group_names = array();
foreach($rows as $key => $row) {
$rowData = explode("\t", $row);
if($rowData[0]) {
$group = $rowData[0];
$group_names[$rowData[0]] = true;
}
if($rowData[1]) {
$subgroup = $rowData[1];
$subgroup_names[$rowData[1]] = true;
}
$table[$group][$subgroup]['jobcount'] = $rowData[2];
if($rowData[3]) $totals[$group]['jobcount'] = $rowData[3];
if($rowData[4]) $totals['grandtotal'] = $rowData[4];
}
$group_names = array_keys($group_names);
asort($group_names);
$subgroup_names = array_keys($subgroup_names);
asort($subgroup_names);
$result = array();
$result['header'] = "<th> </th>";
foreach ($subgroup_names as $subgroup) {
$result['header'] .= "<th>$subgroup</th>";
}
$result['header'] .= "<th>Subtotals</th>";
foreach ($group_names as $group) {
$result[$group] = "<th>$group</th>";
foreach ($subgroup_names as $subgroup) {
$value = isset($table[$group][$subgroup]['jobcount'])?
$table[$group][$subgroup]['jobcount']:
' ';
$result[$group] .= "<td>".$value."</td>";
}
$result[$group] .= "<th>".$totals[$group]['jobcount']."</th>";
}
$result['footer'] = "";
foreach ($subgroup_names as $subgroup) {
$result['footer'] .= "<th> </th>";
}
$result['footer'] .= "<th>Total</th>";
$result['footer'] .= "<th>".$totals['grandtotal']."</th>";
echo "<table><tr>".join($result, "</tr>\n<tr>")."</tr></table>";
?>
答案 0 :(得分:1)
好的,一起来了......这可能不是最漂亮的 - 但输入格式也不是; - )
String txt = '''Big Co.\t2009 Q4\t29\t88\t308
\t2010 Q1\t38\t\t
\t2010 Q4\t21\t\t
Gargantua Inc.\t2009 Q4\t33\t139
\t2010 Q1\t31\t\t
\t2010 Q2\t36\t\t
\t2010 Q3\t39\t\t
Mediocre Ltd.\t2009 Q4\t39\t81
\t2010 Q4\t42\t\t'''
// Parse the text
def summary = txt.split( '\n' ).inject( [] ) { list, row ->
def split = row.split( '\t' ).collect { it.trim() }
while( split.size() < 5 ) split << ''
if( split[ 0 ] ) {
list << [ group:split[ 0 ], grouptotal:split[ 3 ], grandtotal:split[ 4 ], subtotals:[:] ]
}
list[ -1 ].subtotals << [ (split[ 1 ]):split[ 2 ] ]
list
}
// Get a sorted set of all available quarters
def allQuarters = summary.subtotals*.keySet().flatten() as TreeSet
// Then build our html output
def writer = new StringWriter()
def builder = new groovy.xml.MarkupBuilder( writer )
builder.table() {
// Header row
tr {
th "group"
allQuarters.each { q ->
th q
}
th "summary"
}
// Row for each group
summary.each { group ->
tr {
td group.group
allQuarters.each { q ->
def v = group.subtotals."$q"
if( v )
td v
else
td { mkp.yieldUnescaped ' ' }
}
td group.grouptotal
}
}
// Footer row
tr {
td { mkp.yieldUnescaped ' ' }
allQuarters.each { q ->
td { mkp.yieldUnescaped ' ' }
}
td summary.grandtotal.find { it } // Get the first non-empty grandtotal
}
}
println writer.toString()