| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  | <?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.5.6. Using Arrays in Conditional Statements</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="not publican" /><meta xmlns:d="http://docbook.org/ns/docbook" name="package" content="" /><link rel="home" href="index.html" title="SystemTap Beginners Guide" /><link rel="up" href="arrayoperators.html" title="3.5. Array Operations in SystemTap" /><link rel="prev" href="arrayops-deleting.html" title="3.5.5. Clearing/Deleting Arrays and Array Elements" /><link rel="next" href="arrayops-aggregates.html" title="3.5.7. Computing for Statistical Aggregates" /></head><body><p id="title"></p><ul class="docnav top"><li class="previous"><a accesskey="p" href="arrayops-deleting.html"><strong>Prev</strong></a></li><li class="home">SystemTap Beginners Guide</li><li class="next"><a accesskey="n" href="arrayops-aggregates.html"><strong>Next</strong></a></li></ul><div class="section"><div class="titlepage"><div><div><h3 class="title"><a id="arrayops-conditionals"> </a>3.5.6. Using Arrays in Conditional Statements</h3></div></div></div><a id="id3837" class="indexterm"></a><a id="id3840" class="indexterm"></a><a id="id3843" class="indexterm"></a><a id="id3846" class="indexterm"></a><div class="para">You can also use associative arrays in <code class="command">if</code> statements. This is useful if you want to execute a subroutine once a value in the array matches a certain condition. Consider the following example:</div><div class="example"><a id="simplevfsreadprintif"> </a><p class="title"><strong>Example 3.19. vfsreads-print-if-1kb.stp</strong></p><div class="example-contents"><pre class="programlisting">global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { printf("=======\n") foreach (count in reads-) if (reads[count] >= 1024) printf("%s : %dkB \n", count, reads[count]/1024) else printf("%s : %dB \n", count, reads[count]) }</pre></div></div><div class="para">Every three seconds, <a class="xref" href="arrayops-conditionals.html#simplevfsreadprintif">Example 3.19, “vfsreads-print-if-1kb.stp”</a> prints out a list of all processes, along with how many times each process performed a VFS read. If the associated value of a process name is equal or greater than 1024, the <code class="command">if</code> statement in the script converts and prints it out in <code class="command">kB</code>.</div><div class="para"><div xmlns:d="http://docbook.org/ns/docbook" class="title">Testing for Membership</div><a id="id3860" class="indexterm"></a><a id="id3864" class="indexterm"></a><a id="id3868" class="indexterm"></a><a id="id3872" class="indexterm"></a><a id="id3876" class="indexterm"></a>You can also test whether a specific unique key is a member of an array. Further, membership in an array can be used in <code class="command">if</code> statements, as in:</div><pre class="screen">if([<em class="replaceable">index_expression</em>] in <em class="replaceable">array_name</em>) <em class="replaceable">statement</em></pre><div class="para">To illustrate this, consider the following example:</div><div class="example"><a id="simplesimplevfsreadprintifmember"> </a><p class="title"><strong>Example 3.20. vfsreads-stop-on-stapio2.stp</strong></p><div class="example-contents"><pre class="programlisting">global reads probe vfs.read { reads[execname()] ++ } probe timer.s(3) { printf("=======\n") foreach (count in reads+) printf("%s : %d \n", count, reads[count]) if(["stapio"] in reads) { printf("stapio read detected, exiting\n") exit() } }</pre></div></div><div class="para">The <code class="command">if(["stapio"] in reads)</code> statement instructs the script to print <code class="computeroutput">stapio read detected, exiting</code> once the unique key <code class="command">stapio</code> is added to the array <code class="command">reads</code>.</div></div><ul class="docnav"><li class="previous"><a accesskey="p" href="arrayops-deleting.html"><strong>Prev</strong>3.5.5. Clearing/Deleting Arrays and Array Elements</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="arrayops-aggregates.html"><strong>Next</strong>3.5.7. Computing for Statistical Aggregates</a></li></ul></body></html> 
 |