473,322 Members | 1,522 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

make array1 array2 inner a cicle for

I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

how I can to create the names of the variable depending by index i?
z+i not work;

Aug 26 '06 #1
8 1406
padew wrote on 26 aug 2006 in comp.lang.javascript:
I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a
z2 ?
>
how I can to create the names of the variable depending by index i?
z+i not work;
for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')

Beware: eval() is evil.

=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'

try:

var z = new array('a','a','a')

or shorter:

var z = ['a','a','a']

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #2
Il 26 Aug 2006 20:44:09 GMT, Evertjan. ha scritto:
padew wrote on 26 aug 2006 in comp.lang.javascript:
>I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

z2 ?
nobody is perfect :-)

>>
how I can to create the names of the variable depending by index i?
z+i not work;

for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')
ok your example is for:
var z+i="a";

but for
z+i=new Array("a");
is so:
eval( 'var z'+i+' =new Array ("a");')

and I have the object new Array?
=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'
not so;
Aug 26 '06 #3
padew wrote on 26 aug 2006 in comp.lang.javascript:
Il 26 Aug 2006 20:44:09 GMT, Evertjan. ha scritto:
>padew wrote on 26 aug 2006 in comp.lang.javascript:
>>I want make a vfariabl on the fly so:
for (var i = 0; i<2; i++)
{ z+i=new Array("a");
fo to have:
z0=a
z1=a
z1=a

z2 ?
nobody is perfect :-)
Probably so, but your Q is as it is,
so please agnowledge [or not] my surmized correction of it.

"fo to have" is a riddle too.
>>>
how I can to create the names of the variable depending by index i?
z+i not work;

for (var i = 0; i<3; i++)
eval( 'var z'+i+' = "a";')

ok your example is for:
var z+i="a";

but for
z+i=new Array("a");
is so:
eval( 'var z'+i+' =new Array ("a");')

and I have the object new Array?
What do you think, want, and what is the result of you testing?
>
>=====================

or do you mean (this is the way to go!!!):

z[0] = 'a'
z[1] = 'a'
z[2] = 'a'

not so;
A pity.

Your requested naming seems rather pointless.

An array is the perfect and time trusted way
to make a group of variables with a logical connected naming system.

var z = [new Array ('a'),new Array ('a'),new Array ('a')]

or in short:

var z = [['a'],['a'],['a']]

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #4
var z = [['a'],['a'],['a']]
ok the result is this but I have this problem:
the number of every elements ['a'] isn't costant;
is the result of a:

for (var i = 0; i<x.lenght; i++)
var z = [ if lengh 4 here 4 elemts ['a'] ]
only one question:
you use ' ' and not ""; are there difference?

---------------------
for the other
is difficult to communicate without tone voice and/or expression face;
often it has been misinterpreted;
with aggravating circumstance with one does not know well English;
( for to avoid misinterpreted am I¡K )

ok does test and seem work; but test is only with
simple data;

Aug 26 '06 #5
padew wrote on 27 aug 2006 in comp.lang.javascript:
>var z = [['a'],['a'],['a']]


ok the result is this but I have this problem:
the number of every elements ['a'] isn't costant;
is the result of a:

for (var i = 0; i<x.lenght; i++)
When quoting code, copy and paste, do not type.

Language mistakes 'costant' [constant] are acceptable,
coding mistakes 'lenght' in stead of 'length' are not,
since they show an error that should have been corrected in your code.
var z = [ if lengh 4 here 4 elemts ['a'] ]
use:

var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]

This is basic javascript, as shown in any tutorial.
only one question:
you use ' ' and not ""; are there difference?
They can both be used.

This is basic javascript, as shown in any tutorial.

---------------------
for the other
is difficult to communicate without tone voice and/or expression face;
Communication is going on on usenet since 1979, years before the start of
the www, and still going strong.
often it has been misinterpreted;
Yes, but that is the way of things, come over that, no one minds
mistakes, but they should be exposted where they hamper the understanding
of the communication.
with aggravating circumstance with one does not know well English;
My language is Netherlands/Dutch.
( for to avoid misinterpreted am I¡K )
sorry, "I¡K" givers no clue. Are you Italian, as the header suggests?
ok does test and seem work; but test is only with
simple data;
If that is enough for you?

PS: <http://javascript.html.it/guide/lezione/850/array/>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Aug 26 '06 #6
var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]
that you write is:
var z = ['a','a','a']
that I want is:
var z = [['a'],['a'],['a']]

var z = new array()
z[0] = new array()
z[1] = new array()
etc. [any length]
i think, but no sure if I can use in a cycle for ..

ok for all
Aug 26 '06 #7
padew <aa******************@xcxc.xxwrote in comp.lang.javascript:
>var z = new array()
z[0] = ...
z[1] = ...
etc. [any length]

that you write is:
var z = ['a','a','a']
that I want is:
var z = [['a'],['a'],['a']]

var z = new array()
z[0] = new array()
z[1] = new array()
etc. [any length]
i think, but no sure if I can use in a cycle for ..
You want to make an array of arrays (also called a two-dimensional array).

Well yes, an element of an array object can be an array object. And it
can go on ad infinitum.

By 'cycle', do you mean you want to make it in a for-loop??

Easily done:

/* MY_ARRAY_LENGTH is a constant of your choosing,
defined in script or at runtime */
var myArray = new Array(MY_ARRAY_LENGTH);
for (var i = 0; i < myArray.length; i++)
myArray[i] = new Array("a");

You should end up with an array (object) named 'myArray' having
MY_ARRAY_LENGTH count of elements, each element being an array (object)
containing ONE element, being a string constant having a one-character
long string with the value of 'a'.

You could make essentially N - 1 nested for-loops to produce an N-
dimensional array, if within each for-loop, you defined an element of the
existing array to hold a newly constructed array. Do it if there is a
real purpose to doing it.
Notes:

1. The constructor for an array object is "Array()" and NOT "array()"
(See the spelling-counts remark below.)
2. Those familiar with C/C++ who happen to code in Javascript make use of
a great many conventions used by programmers of C/C++. While 'a' and "a"
are optional ways of expressing string constants in Javascript, this is
not so in C and C++, in which the former expresses a character byte value
and the latter is a null-terminated string. It's probably a force of
habit of these programmers to use 'x' for single character constant
definition and "xyz" for multi-character string constant definition.
Javascript is essentially derived from C and C++ and so those familiar
with C/C++ bring their habits/conventions to Javascript.

By the way, you may have noticed that spelling counts in Javascript and
every other scripting/programming language on the planet. Typos are the
very things that break code.

Evertjan is exactly right: rather than re-type code, do a copy-and-paste
of the code that you are working with. It is often typos (including and
especially case sensitivity) that confound programmers in their days-long
efforts to find out why code/script is not executing the way they want.

Many of the questions you pose could be answered if you read a
programmer's reference and/or tutorial on core Javascript.

>
ok for all

Aug 27 '06 #8
JRS: In article <1d****************************@40tude.net>, dated Sun,
27 Aug 2006 00:05:09 remote, seen in news:comp.lang.javascript, padew
<aa******************@xcxc.xxposted :
>for the other
is difficult to communicate without tone voice and/or expression face;
often it has been misinterpreted;
with aggravating circumstance with one does not know well English;

Then ask also in Italian. Many civilised people have a knowledge of the
language, at least for reading it; and some know it well. All you need
is for one person to understand the question properly and answer it; it
does not matter if some of those in the land injudiciously (& possibly)
discovered by Signor Vespucci fail to comprehend the Italian and are
forced to resort to the "English" version.

Also, news:it.comp.lang.javascript is active.
Consider

var z = []
for (var i = 0; i<4; i++) z[i] = "a"+i
Read the newsgroup FAQ.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 27 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Hal Vaughan | last post by:
I may have my terms mixed up (I'm not a professional programmer), but if I do this: var array1 = new Array("TestOne", "TestTwo", "TestThree", "TestFour"); var array2 = new Array(); var test1...
6
by: José Joye | last post by:
I have to compare 2 byte and I must be sure that they are fully identic. I have to perform this check about 1000 times per minute and on arrays that are between 100-200K in size. In that sense,...
5
by: tshad | last post by:
How do you easily make a copy of an arraylist? If you do: arrayList2 = arrayList1 You get a pointer so that if you clear arrayList2 (arrayList2.Clear) - arrayList1 is also cleared. I...
2
by: Summercool | last post by:
I wonder in PHP5, can we use $array1 = $array2 as if it is a class? i think right now, the whole array is copied (by key and value)... and i want to assign by reference but NOT in the alias...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.