| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16  | <?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 xmlns:d="http://docbook.org/ns/docbook">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 xmlns:d="http://docbook.org/ns/docbook" name="generator" content="publican v4.1.3" /><meta xmlns:d="http://docbook.org/ns/docbook" name="package" content="SystemTap-SystemTap_Beginners_Guide-2.6-en-US-2.0-1" /><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="https://fedorahosted.org/publican"><img alt="Product Site" src="Common_Content/images//image_left.png" /></a><a class="right" href="https://fedorahosted.org/publican"><img alt="Documentation Site" src="Common_Content/images//image_right.png" /></a></p><ul class="docnav top"><li class="previous"><a accesskey="p" href="commandlineargssect.html"><strong>Prev</strong></a></li><li class="home">SystemTap Beginners Guide</li><li class="next"><a accesskey="n" href="arrayoperators.html"><strong>Next</strong></a></li></ul><div xml:lang="en-US" class="section" lang="en-US"><div class="titlepage"><div><div><h2 class="title"><a id="associativearrays"> </a>3.4. Associative Arrays</h2></div></div></div><a id="idm214007233712" class="indexterm"></a><a id="idm214018905072" 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="idm214018799152" class="indexterm"></a><a id="idm213979340784" class="indexterm"></a><a id="idm214007089824" class="indexterm"></a><a id="idm214017493712" class="indexterm"></a><a id="idm213969444112" class="indexterm"></a><a id="idm214016959840" class="indexterm"></a><a id="idm214026583152" class="indexterm"></a><a id="idm214009641888" 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="idm213975388304" class="indexterm"></a><a id="idm214021154928" class="indexterm"></a><a id="idm214026344944" class="indexterm"></a><pre class="screen"><em class="replaceable">array_name</em>[<em class="replaceable">index_expression</em>]</pre><div class="para">	Here, the <code class="command"><em class="replaceable">array_name</em></code> is any arbitrary name the array uses. The <code class="command"><em class="replaceable">index_expression</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 <code class="command">tom</code>, <code class="command">dick</code>, and <code class="command">harry</code> (which are unique keys). To assign them the ages (associated values) of 23, 24, and 25 respectively, we'd use the following array statements: </div><a id="idm214012568416" class="indexterm"></a><a id="idm214010602368" class="indexterm"></a><div class="example"><a id="arraysimplestexample"> </a><p class="title"><strong>Example 3.13. Basic Array Statements</strong></p><div class="example-contents"><pre class="screen">foo["tom"] = 23 foo["dick"] = 24 foo["harry"] = 25</pre></div></div><div class="para">	You can specify up to nine index expressions 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 xmlns:d="http://docbook.org/ns/docbook" class="important"><div class="admonition_header"><p><strong>Important</strong></p></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> 
 |