From: Thomas 'PointedEars' Lahn on
Thomas 'PointedEars' Lahn wrote:
> Thadeu de Paula wrote:
>> function arrayMember (value,list) {
>> for (var i in list) if (list[i]===value) {
>> return i;
>> break;
>> };
>> return false;
>> };
>
> This is error-prone for the reasons I gave in
> <news:480E665B.6050506(a)PointedEars.de>

Sorry, I meant <news:480E63A9.9030705(a)PointedEars.de>.


PointedEars
From: Dr J R Stockton on
In comp.lang.javascript message <074fe8ad-e977-4925-9aef-8926efc78b31(a)8g
2000hse.googlegroups.com>, Tue, 22 Apr 2008 11:43:14, Mike
<mpearl1(a)gmail.com> posted:
>I have a list of numbers, e.g., (1,3,4,5,8,16,20), and am trying to
>create a simple IF statement to see if the value is in that list. Is
>there an easier or more efficient way, than the sample code below, to
>do it?

Yes, undoubtedly.

If the list is unordered, a full scan is unavoidable; it should be
(assuming the list is in an Array) scanned with a while loop scanning
down only until the value is found.

If the list is ordered, I guess that a binary chop search **might** be
more efficient, depending on the JavaScript implementation.

If the list is to be searched many times, and is not too sparse, it
could well be worth inverting it with, say,
A = [] ; J = L.length ; while (J--) A[L[J]] = 1
since your test for value V then becomes just
if (A[V])
and the internal code for that ought to be efficient. Test it.

Results may depend on how sparse the original list is, as well as how
long; there could be a considerable difference in speed between the
lists (0, 3, 6, 9) and (0, 3e3, 6e6, 9e9).

If the list is a String of separated numbers, then use IndexOf or a
RegExp, remembering that the search must, unless the numbers are padded
to constant length, seek also the separators. If the search is to be
done many times, sort the entries, split the string, and look only in
the right part - when seeking information on Gnus in a large
encyclopaedia, one first selects the volumes containing the G-words, and
does not read about Aardvarks or Eels or Oxen /en route/.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, nr London UK. ?@merlyn.demon.co.uk IE7 FF2 Op9 Sf3
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
From: Tom Cole on
On Apr 23, 1:47 pm, Dr J R Stockton <j...(a)merlyn.demon.co.uk> wrote:
> In comp.lang.javascript message <074fe8ad-e977-4925-9aef-8926efc78b31(a)8g
> 2000hse.googlegroups.com>, Tue, 22 Apr 2008 11:43:14, Mike
> <mpea...(a)gmail.com> posted:
>
> >I have a list of numbers, e.g., (1,3,4,5,8,16,20), and am trying to
> >create a simple IF statement to see if the value is in that list.  Is
> >there an easier or more efficient way, than the sample code below, to
> >do it?
>
> Yes, undoubtedly.
>
> If the list is unordered, a full scan is unavoidable; it should be
> (assuming the list is in an Array) scanned with a while loop scanning
> down only until the value is found.
>
> If the list is ordered, I guess that a binary chop search **might** be
> more efficient, depending on the JavaScript implementation.
>
> If the list is to be searched many times, and is not too sparse, it
> could well be worth inverting it with, say,
>   A = [] ; J = L.length ; while (J--) A[L[J]] = 1
> since your test for value V then becomes just
>   if (A[V])
> and the internal code for that ought to be efficient.  Test it.
>
> Results may depend on how sparse the original list is, as well as how
> long; there could be a considerable difference in speed between the
> lists (0, 3, 6, 9) and (0, 3e3, 6e6, 9e9).
>
> If the list is a String of separated numbers, then use IndexOf or a
> RegExp, remembering that the search must, unless the numbers are padded
> to constant length, seek also the separators.  If the search is to be
> done many times, sort the entries, split the string, and look only in
> the right part - when seeking information on Gnus in a large
> encyclopaedia, one first selects the volumes containing the G-words, and
> does not read about Aardvarks or Eels or Oxen /en route/.
>
> It's a good idea to read the newsgroup c.l.j and its FAQ.  See below.
>
> --
>  (c) John Stockton, nr London UK.   ?...@merlyn.demon.co.uk     IE7 FF2 Op9 Sf3
>  news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
>  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
>  <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

Just to toss another option out there...what about RegExp?

function doCheck(value, array) {
return (new RegExp('^(' + array.join('|') + ')$').test(value));
}

Send it the value to look for and the array of values...


From: Thomas 'PointedEars' Lahn on
Tom Cole wrote:
> Just to toss another option out there...what about RegExp?

Is there a point in your repeating your suggestion ...

> function doCheck(value, array) {
> return (new RegExp('^(' + array.join('|') + ')$').test(value));
> }

.... already made in
<news:c62a5dd5-58b6-4218-a4d9-30623c0c5bc2(a)y21g2000hsf.googlegroups.com>?

> Send it the value to look for and the array of values...

See <news:480E6AB3.40300(a)PointedEars.de>.

And please trim your quotes.


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
From: Tom Cole on
On Apr 23, 7:14 pm, Thomas 'PointedEars' Lahn <PointedE...(a)web.de>
wrote:
> > Just to toss another option out there...what about RegExp?
>
> Is there a point in your repeating your suggestion ...

> ... already made in
> <news:c62a5dd5-58b6-4218-a4d9-30623c0c5bc2(a)y21g2000hsf.googlegroups.com>?
>

So sorry. I thought I had posted that before but didn't see it. My
apologies.