<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>strindexk</title> <link rel="stylesheet" type="text/css" href="csound.css" /> <meta name="generator" content="DocBook XSL Stylesheets V1.76.1" /> <link rel="home" href="index.html" title="The Canonical Csound Reference Manual" /> <link rel="up" href="OpcodesTop.html" title="Orchestra Opcodes and Operators" /> <link rel="prev" href="strindex.html" title="strindex" /> <link rel="next" href="strlen.html" title="strlen" /> </head> <body> <div class="navheader"> <table width="100%" summary="Navigation header"> <tr> <th colspan="3" align="center">strindexk</th> </tr> <tr> <td width="20%" align="left"><a accesskey="p" href="strindex.html">Prev</a> </td> <th width="60%" align="center">Orchestra Opcodes and Operators</th> <td width="20%" align="right"> <a accesskey="n" href="strlen.html">Next</a></td> </tr> </table> <hr /> </div> <div class="refentry" title="strindexk"> <a id="strindexk"></a> <div class="titlepage"></div> <a id="Indexstrindexk" class="indexterm"></a> <div class="refnamediv"> <h2> <span class="refentrytitle">strindexk</span> </h2> <p>strindexk — Return the position of the first occurence of a string in another string </p> </div> <div class="refsect1" title="Description"> <a id="idp46434440"></a> <h2>Description</h2> <p> Return the position of the first occurence of S2 in S1, or -1 if not found. If S2 is empty, 0 is returned. strindexk runs both at init and performance time. </p> </div> <div class="refsect1" title="Syntax"> <a id="idp46435224"></a> <h2>Syntax</h2> <pre class="synopsis">kpos <span class="command"><strong>strindexk</strong></span> S1, S2</pre> </div> <div class="refsect1" title="Examples"> <a id="idp46437944"></a> <h2>Examples</h2> <p> Here is an example of the strindexk opcode. It uses the file <a class="ulink" href="examples/strindexk.csd" target="_top"><em class="citetitle">strindexk.csd</em></a>. </p> <div class="example"> <a id="idp46438936"></a> <p class="title"> <strong>Example 831. Example of the strindexk opcode.</strong> </p> <div class="example-contents"> <p>See the sections <a class="link" href="UsingRealTime.html" title="Real-Time Audio"><em class="citetitle">Real-time Audio</em></a> and <a class="link" href="CommandFlags.html" title="Csound command line"><em class="citetitle">Command Line Flags</em></a> for more information on using command line flags.</p> <pre class="programlisting"> <span class="csdtag"><CsoundSynthesizer></span> <span class="csdtag"><CsOptions></span> <span class="comment">; Select audio/midi flags here according to platform</span> -odac <span class="comment">;;;realtime audio out</span> <span class="comment">;-iadc ;;;uncomment -iadc if realtime audio input is needed too</span> <span class="comment">; For Non-realtime ouput leave only the line below:</span> <span class="comment">; -o strindexk.wav -W ;;; for file output any platform</span> <span class="csdtag"></CsOptions></span> <span class="csdtag"><CsInstruments></span> <span class="ohdr">sr</span> <span class="op">=</span> 44100 <span class="ohdr">ksmps</span> <span class="op">=</span> 32 <span class="ohdr">nchnls</span> <span class="op">=</span> 2 <span class="ohdr">0dbfs</span> <span class="op">=</span> 1 <span class="ohdr">seed</span> 0 <span class="oblock">instr</span> 1 <span class="comment">;get one element of the input string whenever the metro</span> <span class="comment">;triggers, and call a subinstrument to play the file</span> Smember <span class="opc">strget</span> p4 istrlen <span class="opc">strlen</span> Smember kprint <span class="opc">init</span> 0 ktrig <span class="opc">metro</span> .5 <span class="comment">;whenever the trigger gives signal</span> <span class="octrl">if</span> ktrig <span class="op">==</span> 1 then <span class="comment">;choose a random element (0, 1 or 2)</span> kel <span class="opc">random</span> 0, 2.9999 kel <span class="op">=</span> <span class="opc">int</span>(kel) <span class="comment">;make a copy for leaving Smember intact</span> Scopy <span class="opc">strcpyk</span> Smember <span class="comment">;set the initial index for reading substrings</span> kndx <span class="op">=</span> 0 <span class="comment">;set counter for searching the element</span> kcount <span class="op">=</span> 0 <span class="comment">;start looping over the elements in Smember</span> <span class="olabel">loop</span><span class="op">:</span> kdelim <span class="opc">strindexk</span> Scopy, "<span class="op">:</span>" <span class="comment">;as long as ":" occurs in Scopy, do:</span> <span class="octrl">if</span> kdelim <span class="op">></span> 0 then <span class="comment">;if this is the element to get</span> <span class="octrl">if</span> kel <span class="op">==</span> kcount then <span class="comment">;read it as substring</span> Sfile <span class="opc">strsubk</span> Scopy, kndx, kdelim kprint <span class="op">=</span> kprint<span class="op">+</span>1 <span class="comment">;and jump out</span> <span class="octrl">kgoto</span> <span class="olabel">call</span> <span class="comment">;if not</span> <span class="octrl">else</span> <span class="comment">;cut off this element from Scopy</span> Scopy <span class="opc">strsubk</span> Scopy, kdelim<span class="op">+</span>1, istrlen <span class="octrl">endif</span> <span class="comment">;if no element has been found,go back to loop </span> <span class="comment">;and look for the next element</span> kcount <span class="op">=</span> kcount<span class="op">+</span>1 <span class="octrl">kgoto</span> <span class="olabel">loop</span> <span class="comment">;if there is no delimiter left, the rest is the searched element</span> <span class="octrl">else</span> Sfile <span class="opc">strcpyk</span> Scopy <span class="octrl">endif</span> <span class="olabel">call</span><span class="op">:</span> <span class="comment">;print the result, call the subinstrument and play the file</span> <span class="opc">printf</span> "kel <span class="op">=</span> <span class="op">%</span>d, file <span class="op">=</span> '<span class="op">%</span>s'\n", ktrig<span class="op">+</span>kprint, kel, Sfile S_call <span class="opc">sprintfk</span> {{i 2 0 1 "<span class="op">%</span>s"}}, Sfile <span class="opc">scoreline</span> S_call, ktrig <span class="octrl">endif</span> <span class="oblock">endin</span> <span class="oblock">instr</span> 2 <span class="comment">;play</span> Sfile <span class="opc">strget</span> p4 ilen <span class="opc">filelen</span> Sfile p3 <span class="op">=</span> ilen asig <span class="opc">soundin</span> Sfile <span class="opc">outs</span> asig, asig <span class="oblock">endin</span> <span class="csdtag"></CsInstruments></span> <span class="csdtag"><CsScore></span> <span class="stamnt">i</span> 1 0 30 "mary.wav:fox.wav:beats.wav" <span class="stamnt">e</span> <span class="csdtag"></CsScore></span> <span class="csdtag"></CsoundSynthesizer></span> </pre> </div> </div> <p><br class="example-break" /> </p> </div> <div class="refsect1" title="See also"> <a id="idp46441216"></a> <h2>See also</h2> <p> <a class="link" href="strindex.html" title="strindex"> <em class="citetitle">strindex</em> </a> </p> </div> <div class="refsect1" title="Credits"> <a id="idp46460640"></a> <h2>Credits</h2> <p> </p> <table border="0" summary="Simple list" class="simplelist"> <tr> <td>Author: Istvan Varga</td> </tr> <tr> <td>2006</td> </tr> </table> <p> </p> <p>New in version 5.02</p> </div> </div> <div class="navfooter"> <hr /> <table width="100%" summary="Navigation footer"> <tr> <td width="40%" align="left"><a accesskey="p" href="strindex.html">Prev</a> </td> <td width="20%" align="center"> <a accesskey="u" href="OpcodesTop.html">Up</a> </td> <td width="40%" align="right"> <a accesskey="n" href="strlen.html">Next</a></td> </tr> <tr> <td width="40%" align="left" valign="top">strindex </td> <td width="20%" align="center"> <a accesskey="h" href="index.html">Home</a> </td> <td width="40%" align="right" valign="top"> strlen</td> </tr> </table> </div> </body> </html>