From: HerbF on 25 Jun 2010 02:16 I have a variable constructed as: varValue = "A";"B";..."N" How do I count how many elements, A, B,...n? Currently, I'm doing: varArray = varValue.split(";"); varEls = varArray.length; Is this reliable, or is there a better way? TIA, H-
From: rf on 25 Jun 2010 02:42 <HerbF(a)earthlink.net> wrote in message news:neh8265546m3g9aqt0lenjar1fof8s77n5(a)4ax.com... >I have a variable constructed as: > > varValue = "A";"B";..."N" You don't have what you think you do. What you do have is three statements: varValue = "A"; "B"; "N"; The first assigns the string "A" to the variable varValue. The next two statements do nothing. The strings are parsed and then discarded. Did you perchance mean > varValue = "A;B;...N"; > How do I count how many elements, A, B,...n? > > Currently, I'm doing: > > varArray = varValue.split(";"); Which will give you an array with a single element containing the string "A". > varEls = varArray.length; > > Is this reliable, or is there a better way? Depending on what you are wanting to do.
From: yukabuk on 25 Jun 2010 09:17 On Jun 25, 7:16 am, He...(a)earthlink.net wrote: > I have a variable constructed as: > > varValue = "A";"B";..."N" > > How do I count how many elements, A, B,...n? > > Currently, I'm doing: > > varArray = varValue.split(";"); > varEls = varArray.length; > > Is this reliable, or is there a better way? > > TIA, > H- Where did you learn to 'write' JavaScript? I think you want something like this... var varValue = "A;B;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z"; alert(varValue.split(";").length); Graham Vincent
From: HerbF on 25 Jun 2010 09:37 rf wrote: > ><HerbF(a)earthlink.net> wrote in message >news:neh8265546m3g9aqt0lenjar1fof8s77n5(a)4ax.com... >>I have a variable constructed as: >> >> varValue = "A";"B";..."N" > >You don't have what you think you do. What you do have is three statements: > >varValue = "A"; > >"B"; > >"N"; No semicolon after the last variable in quotes. > >The first assigns the string "A" to the variable varValue. > >The next two statements do nothing. The strings are parsed and then >discarded. > >Did you perchance mean > >> varValue = "A;B;...N"; No. When I print varValue I get "A";"B";...."N" > >> How do I count how many elements, A, B,...n? >> >> Currently, I'm doing: >> >> varArray = varValue.split(";"); > >Which will give you an array with a single element containing the string >"A". When I print varArray I get "A","B",..."N" where the semicolons have been replaced by commas. > >> varEls = varArray.length; This gives me the correct result. >> >> Is this reliable, or is there a better way? > >Depending on what you are wanting to do. H-
From: Richard Cornford on 25 Jun 2010 09:57 On Jun 25, 2:37 pm, He...(a)earthlink.net wrote: > rf wrote: >><He...(a)earthlink.net> wrote: >>>I have a variable constructed as: > >>> varValue = "A";"B";..."N" > >>You don't have what you think you do. What you do have is three >> statements: > >>varValue = "A"; > >>"B"; > >>"N"; > > No semicolon after the last variable in quotes. <snip> If you mean that you did not put a semicolon in that position then it does not matter as javascript's automatic semicolon insertion will have added one for correct parsing, so, as rf said, you have three statements. Richard.
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: DOM timers and the system clock. Next: Vertical scroll bar moving a bit left in IE |