0

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'
5
  • 5
    new Array(3, 7) creates an array with the values [3, 7]. I don't think that's what you want. Commented Aug 28, 2012 at 18:17
  • 1
    in javascript, arrays are dinamic, so you dont have to set the size before. Commented Aug 28, 2012 at 18:18
  • 1
    Just because JavaScript looks like C# or Java doesn't mean that you can use identical syntax. LigneTab[0,4] is invalid, and should probably be LigneTab[0][4]. Commented Aug 28, 2012 at 18:18
  • oh! ok, i fix that : UltraEdit.messageBox(LigneTab[0],[4]); Commented Aug 28, 2012 at 18:23
  • new Array(3,7) does not make an array with 4 Lines, 7 Items. Commented Aug 28, 2012 at 18:23

3 Answers 3

1

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"]
Sign up to request clarification or add additional context in comments.

Comments

0

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

Thats what i want; Sorry i don't figure this : For example i want to put the second line : var Ligne = ("55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"); var Ligne2 = ("55074 5453526.0731 236758.7675 478.6960 POA 2012-08-14 GM33P086"); LigneTab[0] = Ligne.split(reg); LigneTab[1] = Ligne2.split(reg)
That should work just fine. Also, note the last line of my example code, it shows the proper way to read from a multidimensional array
-WildcatQuebec Edit : Thats right; i need a dimension to put a Line and the second dimension is to put the 7 items :) Thx alot
0

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]);

1 Comment

Ligne = ("55062 5453457.4676 236746.6682 472.4027 POA 2012-08-14 GM33P086"); Ligne2= ("55074 5453526.0731 236758.7675 478.6960 POA 2012-08-14 GM33P086"); var reg=new RegExp(" +", "g"); LigneTab[0]=Ligne.split(reg); LigneTab[1]=Ligne2.split(reg); UltraEdit.messageBox(LigneTab[0],[4]); // Show the LigneTab[0] Item 4 POA

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.