table.html
来自「perl教程」· HTML 代码 · 共 225 行
HTML
225 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>ActiveState::Table - Simple table class</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>ActiveState::Table - Simple table class</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#synopsis">SYNOPSIS</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<li><a href="#bugs">BUGS</a></li>
<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>ActiveState::Table - Simple table class</p>
<p>
</p>
<hr />
<h1><a name="synopsis">SYNOPSIS</a></h1>
<pre>
<span class="variable">$t</span> <span class="operator">=</span> <span class="variable">ActiveState::Table</span><span class="operator">-></span><span class="variable">new</span><span class="operator">;</span>
<span class="variable">$t</span><span class="operator">-></span><span class="variable">add_row</span><span class="operator">(</span><span class="operator">{</span> <span class="string">a</span> <span class="operator">=></span> <span class="number">1</span><span class="operator">,</span> <span class="string">b</span> <span class="operator">=></span> <span class="number">2</span> <span class="operator">}</span><span class="operator">);</span>
<span class="keyword">print</span> <span class="variable">$t</span><span class="operator">-></span><span class="variable">as_csv</span><span class="operator">;</span>
</pre>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>Instances of the <code>ActiveState::Table</code> class represent a 2 dimensional
table of fields (or columns if you wish) and rows. The fields are
ordered and have case-sensitive names. The rows are numbered.</p>
<p>The following methods are provided:</p>
<dl>
<dt><strong><a name="item_new">$t = ActiveState::Table->new</a></strong>
<dd>
<p>This creates a new empty table object.</p>
</dd>
</li>
<dt><strong><a name="item_fields">$t->fields</a></strong>
<dd>
<p>This returns the current field names. In scalar context it returns
the number of fields.</p>
</dd>
</li>
<dt><strong><a name="item_rows">$t->rows</a></strong>
<dd>
<p>The returns the current rows. Each row is returned as reference to an
array of values in the same order as the fields. The array might be
shorter than the number of fields, when the trailing values are <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
</dd>
<dd>
<p>In scalar context it returns the number of rows in the table.</p>
</dd>
</li>
<dt><strong><a name="item_fetchrow">$t->fetchrow( $index )</a></strong>
<dd>
<p>This returns the given row. An array reference is returned in scalar
context. The array might be shorter than the number of fields, when
the trailing values are <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
</dd>
<dd>
<p>In list context the values are returned one by one. There will be as
many values as there are fields in the table. Some values might be
<a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>.</p>
</dd>
<dd>
<p>If there is no row with the given $index, then <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned in
scalar context and the empty list in list context.</p>
</dd>
</li>
<dt><strong><a name="item_fetchrow_arrayref">$t->fetchrow_arrayref( $index )</a></strong>
<dd>
<p>Same as <a href="#item_fetchrow"><code>fetchrow()</code></a> but will return an array reference even in list
context.</p>
</dd>
</li>
<dt><strong><a name="item_fetchrow_hashref">$t->fetchrow_hashref( $index )</a></strong>
<dd>
<p>This returns the given row. A hash reference is returned with keys
corresponding to the field names and the values corresponding to the
given row. The values might be <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a>, but a key for all the fields
will exist.</p>
</dd>
<dd>
<p>If there is no row with the given $index, then <a href="../../lib/Pod/perlfunc.html#item_undef"><code>undef</code></a> is returned.</p>
</dd>
</li>
<dt><strong><a name="item_add_field">$t->add_field( $field )</a></strong>
<dd>
<p>This adds another field to the table. The field must be a string. If
the field already exists it is not added again, and the <a href="#item_add_field"><code>add_field()</code></a>
call does nothing.</p>
</dd>
<dd>
<p>There is no return value.</p>
</dd>
</li>
<dt><strong><a name="item_add_row">$t->add_row( $row )</a></strong>
<dd>
<p>This adds another row to the table. The row must currently be a hash
reference. If the hash contains new fields they are added
automatically in sorted order. To enforce an order add the fields
before adding rows.</p>
</dd>
<dd>
<p>There is no return value.</p>
</dd>
</li>
<dt><strong><a name="item_sort">$t->sort( $comparator )</a></strong>
<dd>
<p>This will sort the rows of the table using the given $comparator
function to compare elements. The $comparator is called as for perl's
builtin sort function. References to the rows to compare is available
in <code>$::a</code> and <code>$::b</code> in the form returned by
<a href="#item_fetchrow_arrayref"><code>$t->fetchrow_arrayref</code></a>.</p>
</dd>
</li>
<dt><strong><a name="item_as_box">$t->as_box( %options )</a></strong>
<dd>
<p>This formats the table as text and returns it. The following options
might be provided as key/value pairs:</p>
</dd>
<dd>
<pre>
<span class="variable">name</span> <span class="operator">|</span> <span class="variable">default</span>
<span class="operator">---------------------+----------</span>
<span class="variable">align</span> <span class="operator">|</span> <span class="operator">{}</span>
<span class="variable">null</span> <span class="operator">|</span> <span class="string">"NULL"</span>
<span class="variable">show_header</span> <span class="operator">|</span> <span class="number">1</span>
<span class="variable">show_trailer</span> <span class="operator">|</span> <span class="number">1</span>
<span class="variable">box_chars</span> <span class="operator">|</span> <span class="string">"ascii"</span>
<span class="variable">max_width</span> <span class="operator">|</span> <span class="keyword">undef</span>
<span class="operator">---------------------+----------</span>
</pre>
</dd>
<dd>
<p>The <code>align</code> option is a hash with field names as keys and the strings
"left", "right" or "center" as values. Alignment for fields not found
in this hash is "left".</p>
</dd>
<dd>
<p>The <code>box_chars</code> is either the name of a box drawing scheme or the 11
characters to use; starting with the upper left corner and going down
one row at a time to the lower right corner, and finally the
horizontal and vertical line character. The current named schemes are
"dos" and "unicode".</p>
</dd>
<dd>
<p>If <code>max_width</code> is specified it limits how wide the box can get. The
longest fields are truncated until the box is no wider than the
given number of characters. Truncated fields are shown with trailing
"..." or "+" marker.</p>
</dd>
</li>
<dt><strong><a name="item_as_csv">$t->as_csv( %options )</a></strong>
<dd>
<p>This formats the table as a CSV file ("comma-separated-values") and
returns it. The following options might be provided as key/value
pairs:</p>
</dd>
<dd>
<pre>
name | default
---------------------+----------
field_separator | ","
row_separator | "\n"
null | "NULL"
show_header | 1
---------------------+----------</pre>
</dd>
<dd>
<p>Fields that contains the <code>field_separator</code> or the quote character
will be quoted.</p>
</dd>
</li>
</dl>
<p>
</p>
<hr />
<h1><a name="bugs">BUGS</a></h1>
<p>none.</p>
<p>
</p>
<hr />
<h1><a name="see_also">SEE ALSO</a></h1>
<p>RFC 4180 and <a href="http://en.wikipedia.org/wiki/Comma-separated_values">http://en.wikipedia.org/wiki/Comma-separated_values</a></p>
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?