Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Getting data into a 2D array

Community Beginner ,
May 13, 2020 May 13, 2020

I have a CSV file that I can load into my adobe form. It has a format like this:

 adata00;adata01,adata02;adata03;adata04;adata05;adata06;

bdata10;bdata11;bdata12;bdata13;bdata14;bdata15;bdata16;

cdata20;cdata21;cdata22;cdata23;cdata24;cdata25;cdata26;

etc.

I'm loading it into a (hidden) text field called data_lijst. Works fine! Now I'm trying to build a 2D matrix from it. My current code looks like this:

var getData = this.getField("data_lijst").value.split("\r"); // getting the data from the field, splitting it on the rulebrakes and building a one dimensional array. A check if it works in the shape of this code works fine this.getField("checkfield").value = getData[0] which works all the way up to the end of the CSV file (in Excel its approx. 378 lines). It returns nicely: 

adata00;adata01,adata02;adata03;adata04;adata05;adata06;

So now we want to combine that with loop over that array and build a new 2D one: 

var matrix = new Array () //building an empty new array, not sure if this line is needed.
var getData = this.getField("data_lijst").value.split("\r"); //build an array called getData and fill it with 378 elements

for (var i=0; i<getData.length; i++)  //loop over the array
{
getData[i].split(";"); //trying to split every single element (i) of the getData array into a new (sub)array
matrix.push(getData[i]); // filling the new array called matrix with the values assuming getData[i] is now a subarray
}

this.getField("field00").value = matrix[0][0]; // returns a instead of adata
this.getField("field10").value = matrix[1][0]; // returns b instead of bdata


Tried to use slice too and replace the original index i with a new subarray.

for (var i = 0; i < getData.length; i++)
{
var x = new Array(getData[i].split(";")); //make a new array called x
getData.slice(i,(i ++),x); //replace I until i+1 with the new array x, should be an array in an array
}

Same result L
this.getField("field00").value = getData[0][0]; // returns a instead of adata

I am so darn close. Please, how to do this properly? Much appreciated!

 

TOPICS
How to , PDF forms
919
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
1 ACCEPTED SOLUTION
Community Expert ,
May 13, 2020 May 13, 2020

For filling the matrix use:

matrix.push(getData[i].split(";"));

View solution in original post

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 13, 2020 May 13, 2020

For filling the matrix use:

matrix.push(getData[i].split(";"));

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
May 13, 2020 May 13, 2020
LATEST

Many thanks Bernd. I'm surprised how I've missed this and how elegant the solution. I'm just hacking my way though javascript with no formal training and hardly any experience, so I've still got a lot to learn. Thanks for helping me out!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines