Sophie

Sophie

distrib > Fedora > 13 > i386 > media > os > by-pkgid > 95299258dbdf9a86cefd89b97c0d81e5 > files > 96

systemtap-1.2-1.fc13.i686.rpm

<?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" href="./Common_Content/css/default.css" type="text/css" /><meta name="generator" content="publican 1.6" /><meta name="package" content="Systemtap-SystemTap_Beginners_Guide-1.0-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.3. Command-Line Arguments" /><link rel="next" href="arrayoperators.html" title="3.5. Array Operations in SystemTap" /></head><body class=""><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" title="3.4. Associative Arrays" lang="en-US"><div class="titlepage"><div><div><h2 class="title" id="associativearrays">3.4. Associative Arrays</h2></div></div></div><a id="id3105300" class="indexterm"></a><a id="id2822051" 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="id2742368" class="indexterm"></a><a id="id2770403" class="indexterm"></a><a id="id2764251" class="indexterm"></a><a id="id2913877" class="indexterm"></a><a id="id4380557" class="indexterm"></a><a id="id2928498" class="indexterm"></a><a id="id2893834" class="indexterm"></a><a id="id3091206" class="indexterm"></a><div class="para">
		Since associative arrays are normally processed in multiple probes (as we will demonstrate later), they are 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="id2819397" class="indexterm"></a><a id="id2988451" class="indexterm"></a><a id="id2774918" 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="id2848169" class="indexterm"></a><a id="id4349096" class="indexterm"></a><div class="example" id="arraysimplestexample"><div class="example-contents"><pre class="screen">
foo["tom"] = 23
foo["dick"] = 24
foo["harry"] = 25
</pre></div><h6>Example 3.11. Basic Array Statements</h6></div><br class="example-break" /><div class="para">
		You can specify up to 5 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" title="disktop.stp">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"><h2>Important</h2><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><ul class="docnav"><li class="previous"><a accesskey="p" href="commandlineargssect.html"><strong>Prev</strong>3.3.3. 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>