From: HerbF on
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

<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
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
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
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.