I have trouble to put information in a array with Javascript and Split.
var LigneTab= new Array(3,7); //4 Lines, 7 Items
var reg=new RegExp(" +", "g");
Ligne = ("55062 5453457.4676 236746.6682 472.4027 POA 2012年08月14日 GM33P086"); //First Line
LigneTab[0]=Ligne.split(reg); //Split the line in 7 items and place it in line 0
UltraEdit.messageBox(LigneTab[0,4]]); // Debug msgbox from UltraEdit to show the item 4 'POA'
3 Answers 3
In javascript this doesn't have to be that complex:
var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012年08月14日 GM33P086"
,LigneTab = [Ligne.split(/\s+/)];
// now LigneTab[0] is:
// ["55062", "5453457.4676", "236746.6682", "472.4027", ..., "GM33P086"]
Or even:
var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012年08月14日 GM33P086"
.split(/\s+/);
// Ligne[0]:
// ["55062", "5453457.4676", "236746.6682", "472.4027", ..., "GM33P086"]
Comments
Considering the code you posted, I don't see why you need a two-dimensional array. But if you really need one, you are trying here is one of the possible ways to create it and access it:
var LigneTab = []; // one-dimensional for now
var reg=new RegExp(" +", "g");
var Ligne = "55062 5453457.4676 236746.6682 472.4027 POA 2012年08月14日 GM33P086";
LigneTab[0] = Ligne.split(reg);
// Now LigneTab is two-dimensional.
// LigneTab[0] contains another array with 7 items
UltraEdit.messageBox(LigneTab[0][4]]);
3 Comments
First, you initialize the array as [3, 7], and then you replace the zeroth value with the array you actually want, nested:
LigneTab[0]=Ligne.split(reg); //Split the line in 7 items and place it in line 0
So LigneTab is actually [["55062","5453457.4676","236746.6682","472.4027","POA","2012-08-14","GM33P086"], 7] and there is no value at index 4.
Second, if it did have> 4 elements, LigneTab[0,4] wouldn't make much sense, since the expression
0, 4
evaluates to 4, so you might as well just write LigneTab[4].
You probably want this:
var LigneTab = Ligne.split(/\s+/);
UltraEdit.messageBox(LigneTab[4]]); // Debug msgbox from UltraEdit to show the item 4 'POA'
Or, perhaps you intended to have it as a nested list, in which case you want:
var LigneTab[0] = Ligne.split(/\s+/);
UltraEdit.messageBox(LigneTab[0][4]);
new Array(3, 7)creates an array with the values[3, 7]. I don't think that's what you want.LigneTab[0,4]is invalid, and should probably beLigneTab[0][4].new Array(3,7)does not make an array with4 Lines, 7 Items.