473,327 Members | 1,952 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,327 software developers and data experts.

What is wrong with array.concat in this small example

Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/...y.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

for (var i=0; i<6; i++) {
document.write(famname[i] + "<br />");
}

for (var i=0; i<6; i++) {
document.write(famname2[i] + "<br />");
}
Jul 20 '05 #1
5 1809
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
Hi,

Could it be correct that the following code does *not* work because i'm not using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/...5/reference/ar
ray.html#1194827 but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

Your using "famname" as an array and as a string. Try:

var famname3 = famname.concat(famname2);
Jul 20 '05 #2
McKirahan wrote:
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
Hi,

Could it be correct that the following code does *not* work because i'm
not
using the var arr = new Array("a","b","c"); methodology??

Read through


http://devedge.netscape.com/library/...5/reference/ar
ray.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*


Your using "famname" as an array and as a string. Try:

var famname3 = famname.concat(famname2);

That did the trick indeed.
Makes me wonder from time 2 time.

Thx a lot

Cheers

Jul 20 '05 #3
"F. Da Costa" wrote:
Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/...y.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

for (var i=0; i<6; i++) {
document.write(famname[i] + "<br />");
}

for (var i=0; i<6; i++) {
document.write(famname2[i] + "<br />");
}


Of COURSE it concatenates the arrays, but if you look at your "test code" you are simply looping
through 6 elements of the first array and 6 elements of the second array and outputting them.

If you modify your test code to actually output all of the array values, you'll see that famname
contains all the entries from both famname and famname2:

document.write("Contents of famname<br />");
for (var i=0; i<famname.length; i++) {
document.write(famname[i] + "<br />");
}

document.write("Contents of famname2<br />"):
for (var i=0; i<famname2; i++) {
document.write(famname2[i] + "<br />");
}

Better yet:

document.write("<p>Contents of famname<br />" + famname.join("<br />") + "</p>");
document.write("<p>Contents of famname2<br />" + famname2.join("<br />") + "</p>");

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #4
McKirahan wrote:
"F. Da Costa" <da*****@xs4all.nl> wrote in message
news:40*********************@news.xs4all.nl...
Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through

http://devedge.netscape.com/library/...5/reference/ar
ray.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*


Your using "famname" as an array and as a string. Try:


No, he really isn't. Array.concat(Array) returns an Array, so

famname = famname.concat(famname2); concatenates famnam2 to famname, returns the
result, and assigns it to famname. Even *if* this were the case, JavaScript
wouldn't care, it will accept all sorts of new type assignments:

var a = [ "a", "b", "c" ];
a = a.join("<br />");
document.write(a);

a starts life as an Array, a join() is performed on the contents of that Array
and a String is returned, which is assigned to the same variable a. That
variable is then output.

And it works just fine.

I could provide numerous other examples of assigning different variable types to
the same variable name, but the one above is on point for your concern with his
original code.
var famname3 = famname.concat(famname2);


His problem has nothing to do with the assignment, it had to do with his test
output. If you look, he outputs only the first 6 elements of the Array pointed
to by famname. It contains 12 elements (6 from famname, 6 from famname2), but
you never see the 2nd set of 6 elements, because he simply doesn't output them.

He should be using:

famname = [ "a", "b", "c" ];
famname2 = [ "d", "e", "f" ];
famname = famname.concat(famname2);
document.write('<p>famname contains:<br />' + famname.join('<br />') + '</p>');

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp

* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html
Jul 20 '05 #5
Grant Wagner wrote:
"F. Da Costa" wrote:

Hi,

Could it be correct that the following code does *not* work because i'm not
using the var arr = new Array("a","b","c"); methodology??

Read through
http://devedge.netscape.com/library/...y.html#1194827
but there was no mention of particular constructors having to be used.

And if so how does one resolve it (if resolvable)?

TIA
Fermin DCG

==========================
var famname = new Array();
famname[0] = "Jan 0 Egil";
famname[1] = "To1ve";
famname[2] = "He2ge";
famname[3] = "Sta3le";
famname[4] = "Kai 4Jim";
famname[5] = "Bor5ge";

var famname2 = new Array(6);
famname2[0] = "Jan Egil2";
famname2[1] = "Tove2";
famname2[2] = "Hege2";
famname2[3] = "Stale2";
famname2[4] = "Kai Jim2";
famname2[5] = "Borge2";

famname = famname.concat(famname2); *Does not produce the concatenation!*

for (var i=0; i<6; i++) {
document.write(famname[i] + "<br />");
}

for (var i=0; i<6; i++) {
document.write(famname2[i] + "<br />");
}

Of COURSE it concatenates the arrays, but if you look at your "test code" you are simply looping
through 6 elements of the first array and 6 elements of the second array and outputting them.

:) The infamous middle of the night error.
Yep you are very correct. Obviously resolved this whole thing and knee-high
in other muck again.

Thx for the reaction anyway.

If you modify your test code to actually output all of the array values, you'll see that famname
contains all the entries from both famname and famname2:

document.write("Contents of famname<br />");
for (var i=0; i<famname.length; i++) {
document.write(famname[i] + "<br />");
}

document.write("Contents of famname2<br />"):
for (var i=0; i<famname2; i++) {
document.write(famname2[i] + "<br />");
}

Better yet:

document.write("<p>Contents of famname<br />" + famname.join("<br />") + "</p>");
document.write("<p>Contents of famname2<br />" + famname2.join("<br />") + "</p>");

--
| Grant Wagner <gw*****@agricoreunited.com>

* Client-side Javascript and Netscape 4 DOM Reference available at:
* http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
* http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
* http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
* http://www.mozilla.org/docs/web-deve...upgrade_2.html

Jul 20 '05 #6

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

Similar topics

3
by: rl | last post by:
Hi out there, I'd like to know sth about the costs of a function call in php and the handling of character arrays (init size, enlargement steps of allocated memory, technique on enlargement ->...
4
by: F. Da Costa | last post by:
Hi, I was wondering whether someone could enlighten me as to the reason why the slice does not work in IE when the arr is passed in properly. Checked the values in the srcArr and they are...
5
by: Andrew Poulos | last post by:
If I'm searching for an occurance of a value in a multi-dimensional array how can I get it's index returned as an array, if found? For example, if: foo = new Array(); foo = , 5, , 9, 10]; ...
2
by: vvenk | last post by:
Hello: I have the following code: Dim lsFilter As String = Nothing Select Case rblStatus.SelectedItem.Value Case "S" lsFilter = String.Concat("D_STAT_CD_C = '", "N'") Case "R" lsFilter =...
3
by: Mythran | last post by:
Out of curiosity, only, which is recommended for SHORT concatenation...or concatenating two or three strings that are relatively small in size? Dim a As String = "bah" Dim b As String = "bah2"...
7
by: Leonel Gayard | last post by:
Hi all, I had to write a small script, and I did it in python instead of shell-script. My script takes some arguments from the command line, like this. import sys args = sys.argv if args ==...
6
by: Jeff Gardner | last post by:
Greetings: I am attempting to get conditional output based on POSTed form data. If the posted value is either the key or value of an array, $x=key and $q=foo. elseif it is neither key nor...
23
by: anon.asdf | last post by:
Hello! In the following code-snippet, is it possible to initialize each element of arr, with STRUCT_INIT? struct mystruct { int a; char b; };
1
by: agendum97 | last post by:
MSDN says splice has the following arguments: arrayObj.splice(start, deleteCount, ]]]) Thus I can insert items into an array using splice. But how do I insert an entire array? For example:...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: 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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.