XML Feeds

.

[javascript-dev] Tasks and String manipulations

jasch   j at jasch.ch
Sun Mar 16 15:15:34 MDT 2008


here's my take on this:
you need to put the result of the string splitting operation into an  
array. apart from that your code works:

result = newstr.split(" ",  5);

hth

/*j


max v2;
#N vpatcher 825 44 1264 278;
#P window setfont "Sans Serif" 9.;
#P message 68 86 116 196617 setFilename testfile.txt;
#P number 68 45 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P message 68 65 75 196617 setInterval \$1;
#P number 48 159 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 95 159 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 142 159 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 189 159 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P number 236 159 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P button 48 25 15 0;
#P newex 48 128 194 196617 js biodata_reader.js 0 0 testfile.txt 500;
#P comment 66 25 35 196617 parse;
#P connect 10 0 1 0;
#P connect 8 0 1 0;
#P connect 2 0 1 0;
#P connect 1 0 7 0;
#P connect 9 0 8 0;
#P connect 1 1 6 0;
#P connect 1 2 5 0;
#P connect 1 3 4 0;
#P connect 1 4 3 0;
#P pop;

/////////// testfile.txt ///////////

0, 110 9 220 218;
1, 110 10 218 218;
2, 110 11 218 218;
3, 110 12 218 218;
4, 110 13 218 218;
5, 110 14 218 218;
6, 110 15 218 216;
7, 110 16 218 220;
8, 110 17 220 220;
9, 110 18 220 220;
10, 110 19 220 220;

/////////// biodata_reader.js ///////////

/*
* Reading through BioData Text File
*
* by garth paine    March 2008
* http://www.activatedspace.com  and http://www.syncsonics.com
*/

// global code
inlets = 1;
outlets = 5;
autowatch = 1;

var f = new File();
var t = new Task();

var myInterval = 0;
var myFilename = new String("");

var newstr = new String("");
var result = new Array(); // Array to contain split string

if (jsarguments.length > 0)
//	nada = jsarguments[1];
if (jsarguments.length > 1)
//	nada = jsarguments[2];
if (jsarguments.length > 2)
	myFilename = jsarguments[3];
if (jsarguments.length > 3)
	myInterval = jsarguments[4];
		
// re = /\(?\d{4}\)?([,])\d{2}\1\d{2}\1\d{2}\1\d{2}/;

function bang()
{
	post("------------------------------------------------------- \n");
	// fileName = jsarguments[3];
	post("Reding from file: "+ myFilename + "\n");
	f = new File(myFilename);
	f.open ();

	if (f.isopen) {
		newstr = "start";
		f.position = 0;
		t = new Task(readFile, this);
//		t.interval = jsarguments[4]; // time interval set as argument in  
object
		t.interval = myInterval; 	// time interval set as argument in object  
or by message
		t.repeat(); // repeat
	}
}

function stopTask()
{
	t.cancel();
}

function readFile()
{
	newstr = f.readline();
	if (newstr != null) {
		result = newstr.split(" ",  5); // read split string into result Array
		post("result " + result + "\n");
		if (result != null) {
			for(i=0; i < 5; i++) {
				outlet(i, parseInt(result[i]));
			}
			//newstr = re.exec(aString); possible approach using expressions
		}
	} else {
		t.cancel(); // stops task when nothing remains to be read in textfile
		post("the file " + fileName +" is not open.");
	}
}

function list()            //just incase a list is sent in
{
	var aList = arrayfromargs(arguments);
	post("------ you sent me a list ---------" + "\n" +aList +"\n");
}

function setInterval(a)
{
	myInterval = a;
	post("new Interval is "+myInterval+"\n");
}

function setFilename(fn)
{
	myFilename = fn;
	post("new Filename is "+myFilename+"\n")
}

/* EOF */




More information about the javascript-dev mailing list