From: HerbF on 26 Jun 2010 12:11 Thomas 'PointedEars' Lahn wrote: >Lasse Reichstein Nielsen wrote: > >> HerbF(a)earthlink.net writes: >>> varArray = varValue.split(";"); >>> varEls = varArray.length; >>> >>> Is this reliable, or is there a better way? >> >> If there are no other semicolons than the ones separating your >> elements, it should split the string into those elements. >> >> It's a waste of time and space to make all those small strings >> if you don't need them. >> You could also do something like this: >> >> var count = 0; >> for (var i = varArray.indexOf(";"); >> i >= 0; >> i = varArray.indexOf(";", i + 1)){ >> count++; >> } > >May I suggest > > var count = (varArray.match(/;/g) || "").length + 1; > >instead? ;-) > Works well. Thanks. H-
From: Thomas 'PointedEars' Lahn on 26 Jun 2010 14:07 HerbF(a)earthlink.net wrote: > Thomas 'PointedEars' Lahn wrote: >> May I suggest >> >> var count = (varArray.match(/;/g) || "").length + 1; >> >> instead? ;-) > > Works well. Thanks. Note that it does not work with 0 elements, see my other reply. You are welcome. Please get a real name. 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: Lasse Reichstein Nielsen on 26 Jun 2010 16:34 Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes: > Note that it does not work with 0 elements, see my other reply. The syntax we have been shown so far does not allow for zero elements, so I think that's a perfectly reasonable restriction. /L -- Lasse Reichstein Holst Nielsen 'Javascript frameworks is a disruptive technology'
From: Dr J R Stockton on 26 Jun 2010 13:57 In comp.lang.javascript message <neh8265546m3g9aqt0lenjar1fof8s77n5(a)4ax. com>, Thu, 24 Jun 2010 23:16:10, HerbF(a)earthlink.net posted: [In a string:] >How do I count how many elements, A, B,...n? If the element separators are easily recognised, you can split on the separators and use the size of the array. If the elements themselves are easily recognised, you can split on the elements and use the size of the array. In either case, you need to consider what may happen if there can be separators at the ends of the string. Another approach is to use .replace(RegExp, "") /* or "x" */ and to use either the change in length or the result length : that does not create a large number of probably-unwanted Objects, and might therefore be faster. If the grammar is different, si that eadh element is to be contained in, say, quores it brackets, then adapt accordingly. -- (c) John Stockton, nr London UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. <URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links; <URL:http://www.merlyn.demon.co.uk/clpb-faq.txt> RAH Prins : c.l.p.b mFAQ; <URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.
From: Dr J R Stockton on 27 Jun 2010 16:53 In comp.lang.javascript message <39wat39z.fsf(a)gmail.com>, Sat, 26 Jun 2010 13:57:28, Lasse Reichstein Nielsen <lrn.unread(a)gmail.com> posted: >Thomas 'PointedEars' Lahn <PointedEars(a)web.de> writes: > >> May I suggest >> >> var count = (varArray.match(/;/g) || "").length + 1; >> >> instead? ;-) > >That should be pretty effective. >I like it. Your "replace" method creates one string, however many delimiters it finds; the quoted method creates one string per delimiter. And I think it more likely that an engine might abstain from creating an actual Object in the first case than from creating many in the second. -- (c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME. Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links; Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc. No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
First
|
Prev
|
Pages: 1 2 3 4 5 Prev: DOM timers and the system clock. Next: Vertical scroll bar moving a bit left in IE |