Forums > Javascript

search an array in a js

June 16, 2006 | 8:26 pm

I’m trying to write a function to search an array for a string. This seems like it should work, but it doesn’t.
In the lines:
matched = str.match(lookat);
post("match?",matched,"n");
the max window just posts "jsobject 0". I’d expect it to post the string that was found matching, but it doesn’t. A slightly different version works in some web browser javascripts that I’ve found, but not in Max.
What is it that I’m missing??

Peter.

#P window setfont Geneva 9.;
#P window linecount 1;
#P message 117 110 84 196617 searchit horse 1;
#P newex 116 146 64 196617 js search.js;
#P connect 1 0 0 0;
#P window clipboard copycount 2;

/*————-javascript: search.js————-*/

var tname_items = new Array("the big horse","the small cat","the oddly shaped turtle","a loathesome dove")

function searchit(string,newsearch){

var matched;
var str=string;
if (newsearch) {
var search_tmp=new Array();
var l=0; //ok, (lowercase L) isn’t the wisest choice of names, but….
var searchindex=0;
post ("matching: "+str,"n");
if (string) {
for (var k=0;k
matched=0;
var lookat=tname_items[k];
post("searching…",lookat,"n");
matched = str.match(lookat);
post("match?",matched,"n"); //posts "jsobejct 0" in max window. hmmm….
if (matched) {
search_tmp[l] = file_items[k];
l=l+1;
}
}
}
}
if (matched) {
for (m=0;m
post("matches: "+search_tmp[m],"n");
}
} else {
post("no match","n");
}
}

/*——–end search.js————*/


June 16, 2006 | 9:48 pm

i think you confused the string you want to search and the pattern
you are looking for.
try: matched = lookat.match(str);

afaik string.match() returns an array, even if no match was found.
that’s probably where the "jsobject 0" comes from.
(also the if statement at the end looks like it is not going to do
what you want.)

hth,
volker.


June 16, 2006 | 10:07 pm

see what happens when you stare at something too long! Thanks for the
pointers. here’s a js that actually seems to work…..

/*————begin search.js—————–*/

/*searches an array, finds the indices of matches, and returns items
from another array using those indices
peter nyboer
*/

var tname_items = new Array("the big horse","the small cat","the oddly
shaped turtle","a loathesome dove")
var file_items = new
Array("C:/horse.txt","C:/cat.txt","C:/turtle.txt","C:/dove.t xt")
function searchit(string,newsearch){

var matched=new Array();
var str=string;
if (newsearch) {
var search_tmp=new Array();
var l=0; //ok, (lowercase L) isn’t the wisest choice of names, but….
var searchindex=0;
post ("matching: "+str,"n");
if (string) {
for (var k=0;k
matched=new Array();
var lookat=tname_items[k];
post("searching…",lookat,"n");
matched = lookat.match(str);
if (matched) post("match?",matched.length,"n");
if (matched) {
search_tmp[l] = file_items[k];
l=l+1;
}
}
}
}
if (search_tmp.length>0) {
for (m=0;m
post("matches: "+search_tmp[m],"n");
}
} else {
post("no match","n");
}
}

/*—————–end search.js————-*/


Viewing 3 posts - 1 through 3 (of 3 total)