<?xml version="1.0" encoding="UTF-8"?>
	<rss version="2.0"
		xmlns:content="http://purl.org/rss/1.0/modules/content/"
		xmlns:wfw="http://wellformedweb.org/CommentAPI/"
		xmlns:dc="http://purl.org/dc/elements/1.1/"
		xmlns:atom="http://www.w3.org/2005/Atom"

			>

	<channel>
		<title>Cycling 74  &#187;  Topic: Manipulating paths in JS LiveAPI.</title>
		<atom:link href="http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/feed" rel="self" type="application/rss+xml" />
		<link>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/feed</link>
		<description></description>
		<pubDate>Tue, 18 Jun 2013 21:57:35 +0000</pubDate>
		<generator>http://bbpress.org/?v=2.2.4</generator>
		<language></language>

		
														
					
				<item>
					<guid>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-60327</guid>
					<title><![CDATA[Manipulating paths in JS LiveAPI.]]></title>
					<link>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-60327</link>
					<pubDate>Tue, 29 Nov 2011 01:36:36 +0000</pubDate>
					<dc:creator>Peter Nyboer</dc:creator>

					<description>
						<![CDATA[
						<p>It seems like there&#8217;s some fairly gnarly string operations going on in the JS LiveAPI paths.<br />
Let&#8217;s say I want to get a path from something, and prepare a path, based on what I find&#8230;</p>
<pre><code>function test(){
var api = new LiveAPI();
api.path = &#39;this_device&#39;; //returns sth like "live_set tracks 0 devices 0"
}</code></pre><p>If api.path is<br />
<code>"live_set tracks 0 devices 0"</code><br />
What I&#8217;m interested in is the device to the right of this_device. How can I unravel that result and change that 0, then add a few more things to the path, for example:<br />
<code>"live_set tracks 0 devices 1 chains 0"</code><br />
??<br />
I&#8217;ve tried various tricks of unravelling the string and packing it together into something that I can use to set to api.path, but I keep getting errors.<br />
Any hints?<br />
Peter</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217116</guid>
					<title><![CDATA[Re: Manipulating paths in JS LiveAPI.]]></title>
					<link>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217116</link>
					<pubDate>Wed, 07 Dec 2011 08:24:39 +0000</pubDate>
					<dc:creator>GreaterThanZero</dc:creator>

					<description>
						<![CDATA[
						<p>I don&#8217;t know how efficient this is compared to good old fashioned string manipulation, but I&#8217;ve always found arrays to be more intuitive.</p>
<pre><code>var oldString = "live_set tracks 0 devices 0";
var strArray = oldString.split(" ");</code></pre><p>So now it&#8217;s a list of words (as previously separated by spaces).</p>
<p>being array members now, we can alter the parameters individually.  The &#8220;0&#8243; we want to change was the 6th list item, but we&#8217;re counting up from 0, so it&#8217;s #5&#8230;</p>
<p><code>strArray[5] = "1";</code></p>
<p>Then, when we&#8217;re done, turn the array back into a string.</p>
<p><code>newString = strArray.join(" ");</code></p>
<p>(each element is separated by a spacebar.</p>
<p>And then, appending strings is just the plus sign.</p>
<p>So, something like this:</p>
<pre><code>var oldString = "live_set tracks 0 devices 0";
var strArray = oldString.split(" ");
strArray[5] = "1";
var newString = strArray.join(" ") + " chains 0";</code></pre><p>&#8230;but you knew all that.</p>
<p>The real question is <em>&#8220;why am I getting errors?&#8221;</em> and the answer is <em>&#8220;because you&#8217;re trying to perform math operations on a string.&#8221;</em></p>
<p>&#8220;30&#8243; + 1 does not equal 31.  It equals &#8220;301&#8243;.  That&#8217;s probably out of range, so your code&#8217;s going to behave unpredictably.</p>
<p>likewise&#8230;</p>
<pre><code>var fakeNumber = "30";
fakeNumber++;</code></pre><p>You can&#8217;t increment a string like this.  That&#8217;s an error.</p>
<p>This, however, works:</p>
<pre><code>var fakeNumber = "30";
fakeNumber = Number(fakeNumber) + 1;</code></pre><p>Does that help?</p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217117</guid>
					<title><![CDATA[Re: Manipulating paths in JS LiveAPI.]]></title>
					<link>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217117</link>
					<pubDate>Wed, 07 Dec 2011 09:13:00 +0000</pubDate>
					<dc:creator>GreaterThanZero</dc:creator>

					<description>
						<![CDATA[
						<p>Actually, it looks like api.path also has quotation marks baked in.  That&#8217;ll get in your way.</p>
<p>So, something like this might be less fragile:</p>
<pre><code>function apiTest() {
	var api = new LiveAPI();
	api.path= &#39;this_device&#39;;

	var nextPath = removeQuotes(api.path);
	nextPath = moveToTheRight(nextPath);
	nextPath = appendStuff(nextPath);
	nextPath = replaceQuotes(nextPath);

	post(nextPath);
}

function moveToTheRight(devicePath){
	//split into a more usable array
	var pathArray = devicePath.split(" ");

	// increment the device number
	pathArray[4] = Number(pathArray[4]) + 1;

	//rebuild the string
	return pathArray.join(" ");
}

function removeQuotes(str) {
	var tmpArray = str.split(&#39;"&#39;);
	return tmpArray[1];
}

function replaceQuotes(str) {
	return &#39;"&#39; + str + &#39;"&#39;;
}

function appendStuff(str) {
	return str + " chain 10";
}</code></pre><p>I&#8217;m not sure how to actually pipe that new path back into the api and make it useful to you, but there it is.</p>
<p><em>(worth pointing out: I&#8217;ve got no experience coding for the Live API, and every part of this is probably wrong.  we learn by doing&#8230;)</em></p>
						]]>
					</description>

					
					
				</item>

			
				<item>
					<guid>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217118</guid>
					<title><![CDATA[Re: Manipulating paths in JS LiveAPI.]]></title>
					<link>http://cycling74.com/forums/topic/manipulating-paths-in-js-liveapi/#post-217118</link>
					<pubDate>Wed, 07 Dec 2011 09:25:36 +0000</pubDate>
					<dc:creator>GreaterThanZero</dc:creator>

					<description>
						<![CDATA[
						<p>Okay, that last part was easily researched.  This may not be at all efficient, but it should at least be usable.  I think.</p>
<p>(Doesn&#8217;t look like the quotation marks are actually needed, so we&#8217;re not adding them back anymore.  And my &#8220;removeQuotes&#8221; function was embarrassingly bad, so I rewrote that as well.)</p>
<pre><code>function apiTest(){
	var api = new LiveAPI();
	api.path= &#39;this_device&#39;;

	var nextPath = removeQuotes(api.path);
	nextPath = moveToTheRight(nextPath) + " chain 10";

	//pipe that string back into the LiveAPI
	api = new LiveAPI(, nextPath);

	//and confirm that this worked
	post(api.path);
}

function moveToTheRight(devicePath){
	//split into a more usable array
	var pathArray = devicePath.split(" ");

	// increment the device number
	pathArray[4] = Number(pathArray[4]) + 1;

	//rebuild the string
	return pathArray.join(" ");
}

function removeQuotes(str){
	return str.split(&#39;"&#39;).join("");
}</code></pre>						]]>
					</description>

					
					
				</item>

					
		
	</channel>
	</rss>

