| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15  | <?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>3.4. Associative Arrays</title><link rel="stylesheet" type="text/css" href="Common_Content/css/default.css" /><link rel="stylesheet" media="print" href="Common_Content/css/print.css" type="text/css" /><meta name="generator" content="publican 2.5" /><meta name="package" content="Systemtap-SystemTap_Beginners_Guide-1.7-en-US-2.0-2" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="understanding-how-systemtap-works.html" title="Chapter 3. Understanding How SystemTap Works" /><link rel="prev" href="commandlineargssect.html" title="3.3.4. Command-Line Arguments" /><link rel="next" href="arrayoperators.html" title="3.5. Array Operations in SystemTap" /></head><body><p id="title"><a class="left" href="http://www.fedoraproject.org"><img src="Common_Content/images/image_left.png" alt="Product Site" /></a><a class="right" href="http://docs.fedoraproject.org"><img src="Common_Content/images/image_right.png" alt="Documentation Site" /></a></p><ul class="docnav"><li class="previous"><a accesskey="p" href="commandlineargssect.html"><strong>Prev</strong></a></li><li class="next"><a accesskey="n" href="arrayoperators.html"><strong>Next</strong></a></li></ul><div xml:lang="en-US" class="section" id="associativearrays" lang="en-US"><div class="titlepage"><div><div keep-together.within-column="always"><h2 class="title" id="associativearrays">3.4. Associative Arrays</h2></div></div></div><a id="id687660" class="indexterm"></a><a id="id654003" class="indexterm"></a><div class="para">	SystemTap also supports the use of associative arrays. While an ordinary variable represents a single value, associative arrays can represent a collection of values. Simply put, an associative array is a collection of unique keys; each key in the array has a value associated with it. </div><a id="id578661" class="indexterm"></a><a id="id1166331" class="indexterm"></a><a id="id801732" class="indexterm"></a><a id="id691719" class="indexterm"></a><a id="id1189256" class="indexterm"></a><a id="id496723" class="indexterm"></a><a id="id625286" class="indexterm"></a><a id="id606105" class="indexterm"></a><div class="para">	Since associative arrays are normally processed in multiple probes (as we will demonstrate later), they should be declared as <code class="command">global</code> variables in the SystemTap script. The syntax for accessing an element in an associative array is similar to that of <code class="command">awk</code>, and is as follows: </div><a id="id750410" class="indexterm"></a><a id="id1161006" class="indexterm"></a><a id="id608313" class="indexterm"></a><pre class="screen"><em class="replaceable"><code>array_name</code></em>[<em class="replaceable"><code>index_expression</code></em>]</pre><div class="para">	Here, the <code class="command"><em class="replaceable"><code>array_name</code></em></code> is any arbitrary name the array uses. The <code class="command"><em class="replaceable"><code>index_expression</code></em></code> is used to refer to a specific unique key in the array. To illustrate, let us try to build an array named <code class="command">foo</code> that specifies the ages of three people (i.e. the unique keys): <code class="command">tom</code>, <code class="command">dick</code>, and <code class="command">harry</code>. To assign them the ages (i.e. associated values) of 23, 24, and 25 respectively, we'd use the following array statements: </div><a id="id593253" class="indexterm"></a><a id="id647273" class="indexterm"></a><div class="example" id="arraysimplestexample"><h6>Example 3.13. Basic Array Statements</h6><div class="example-contents"><pre class="screen">foo["tom"] = 23 foo["dick"] = 24 foo["harry"] = 25</pre></div></div><br class="example-break" /><div class="para">	You can specify up to nine index expressons in an array statement, each one delimited by a comma (<code class="command">,</code>). This is useful if you wish to have a key that contains multiple pieces of information. The following line from <a class="xref" href="mainsect-disk.html#scriptdisktop">disktop.stp</a> uses 5 elements for the key: process ID, executable name, user ID, parent process ID, and string "W". It associates the value of <code class="command">devname</code> with that key. </div><pre class="screen">device[pid(),execname(),uid(),ppid(),"W"] = devname</pre><div class="important"><div class="admonition_header"><h2>Important</h2></div><div class="admonition"><div class="para">	All associate arrays must be declared as <code class="command">global</code>, regardless of whether the associate array is used in one or multiple probes. </div></div></div></div><ul class="docnav"><li class="previous"><a accesskey="p" href="commandlineargssect.html"><strong>Prev</strong>3.3.4. Command-Line Arguments</a></li><li class="up"><a accesskey="u" href="#"><strong>Up</strong></a></li><li class="home"><a accesskey="h" href="index.html"><strong>Home</strong></a></li><li class="next"><a accesskey="n" href="arrayoperators.html"><strong>Next</strong>3.5. Array Operations in SystemTap</a></li></ul></body></html> 
 |