String.search problem…

Jan 23, 2008 at 7:47pm

String.search problem…

I am trying to process the lines of a file using “readline”.
I have pasted my code below. For some reason when I call
the method string “search”, I get the following error…

• error: js: (null): Javascript SyntaxError: unterminated character class , line 0
• error: js: error calling function Test

For the life of me, I can not figure this one out. I know
search is a method that exists because I have called
it before with no problems. Anyone have any ideas?

// inlets and outlets
inlets = 1;
outlets = 1;

var IniFile = null;
function Test(filename)
{
if(IniFile != null)
{
IniFile.close();
IniFile = null;
}

IniFile = new File(filename, “read”);
if(IniFile.isopen == false)
{
post(“FileIO Error: could not open file.n”);
return;
}
else
post(“file open.n”);

var OutputStr = “”;
IniFile.position = 0;
post(“IniFile.position = ” + IniFile.position + “n”);
post(“IniFile.eof = ” + IniFile.eof + “n”);
var Result = 0;
var Line = “”;
while(IniFile.position != IniFile.eof)
{
Line = IniFile.readline (256);
post(“input: ” + Line + “n”);
Result = Line.search(“[");
post("search done...n");
if(Result != -1)
{
// remove section delimiters.
Line.replace("[", "");
Line.replace("]“, “”);
OutputStr += Line + ” “;
}
}

outlet(0, OutputStr);
}

#35492
Jan 23, 2008 at 9:34pm

On 23-janv.-08, at 20:48, Anthony Palomba wrote:

> For the life of me, I can not figure this one out. I know
> search is a method that exists because I have called
> it before with no problems. Anyone have any ideas?

I can’t test your code now, but afaik, String.search() expects a
regular expression as argument, but “[” doesn’t look like a RE.

p

_____________________________
Patrick Delges

Centre de Recherches et de Formation Musicales de Wallonie

http://www.crfmw.be/max

#121055
Jan 23, 2008 at 11:45pm

String.search takes a string and searches it for a
specified substring. It returns -1 or the index of the
beginning of that substring. I am pretty sure this works.

One difference is that when I normally use it, I pass
a string in from outside. In this case I am using fileobject.getline.

#121056
Jan 23, 2008 at 11:49pm

On 24 janv. 08, at 00:45, Anthony Palomba wrote:

> String.search takes a string and searches it for a
> specified substring. It returns -1 or the index of the
> beginning of that substring. I am pretty sure this works.

I use it in ej.function, and it works fine.

ej

#121057
Jan 23, 2008 at 11:54pm

On Jan 23, 2008, at 3:45 PM, Anthony Palomba wrote:

>
> String.search takes a string and searches it for a
> specified substring. It returns -1 or the index of the
> beginning of that substring. I am pretty sure this works.

Your error message would suggest it thinks “[" is the beginning of a
regexp charset. So for ordinary strings maybe this is true since they
are the same as their representation as a regular expression.

Try using "[" and "]” –i.e. backslash to escape the brackets…

Let us know what you find.

-Joshua

#121058
Jan 23, 2008 at 11:57pm

Btw, the definitive source:

http://developer.mozilla.org/en/docs/
Core_JavaScript_1.5_Reference:Objects:String:search

It is a regular expression. So you definitely need to escape brackets
and the like.

-Joshua

#121059
Jan 24, 2008 at 12:02am

I was referencing this…

http://www.w3schools.com/jsref/jsref_search.asp

Is this a bad source?

#121060
Jan 24, 2008 at 12:08am

On Jan 23, 2008, at 4:02 PM, Anthony Palomba wrote:

>
> I was referencing this…
> http://www.w3schools.com/jsref/jsref_search.asp
>
> Is this a bad source?

Well it would appear that this isn’t the way it works in JS 1.5 (may
have been the case in previous versions, or they may be glossing over
regexp behavior). Core Javascript book and mozilla site I sent in my
last message are highly recommended

-Joshua

#121061
Jan 24, 2008 at 4:34pm

I went ahead and treated it as a regular expression. Line.search(“[/[]“) does the trick. Thanks for helping
me track this one down.

#121062

You must be logged in to reply to this topic.