# Converting a text file into a multidimensional array of data points

**URL:** https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007
**Category:** Coding Questions
**Created:** [March 30, 2022, 5:43am UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007 "2022-03-30T05:43:39Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![TheSynchronomicon](https://avatars.discourse-cdn.com/v4/letter/t/b487fb/32.png) [@TheSynchronomicon](https://discourse.processing.org/u/TheSynchronomicon)
#### Post date: [March 30, 2022, 5:43am UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/1 "2022-03-30T05:43:39Z")

</div>

I want to be able to convert the following array from a text file

[[-7.5,-7.5,-5.0], [-2.5,-7.5,0.0], [2.5,-7.5,0.0], [7.5,-7.5,-5.0]], ,  
[[-7.5,-2.5,-7.5], [-2.5,-2.5,5.0], [2.5,-2.5,5.0], [7.5,-2.5,-7.5]], ,  
[[-7.5, 2.5, 0.0], [-2.5,2.5,-5.0], [2.5,2.5,-5.0], [7.5, 2.5, 0.0]], ,  
[[-7.5, 7.5,-5.0], [-2.5,7.5,-10.00], [2.5,7.5,-10.00], [7.5,7.5,-5.0]]

into an array that can be used in p5 via createFileInput. Currently, I have split the array into the respective rows using the code below via double commas, but I’m not sure how to add the rows to a new multidimensional array as numbers instead of as a string.

```auto
function loadFile(file){
  array = file.data;
  splitStrings = split(array,', ,'); 
}

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 30, 2022, 8:33am UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/2 "2022-03-30T08:33:15Z")

</div>

The three numbers can be seen as PVector.

So you have lines and each line has 4 PVectors

So you can make a 2D grid of PVector

You need to use `split()` command several times:

- One you did already

- Next at `"],"` (remove [ )

- Next at , to fill the PVector

You can see the list as a list of quads / rectangles in 3D

Chrisir

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [March 30, 2022, 10:57am UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/3 "2022-03-30T10:57:23Z")

</div>

> [@TheSynchronomicon](#):
>
> I’m not sure how to add the rows to a new multidimensional array as numbers instead of as a string.

Hello @TheSynchronomicon,

Reference:  
[https://p5js.org/reference/#/p5/float](https://p5js.org/reference/#/p5/float)

Some hints here that may help:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e9f7be4c90cc6148a79944019375f08af0303cbd.png)

You can look up the references.

Once I can work with the data I then proceed to the next step.  
I leave the multidimensional array with you.

`:)`

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 30, 2022, 7:00pm UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/4 "2022-03-30T19:00:09Z")

</div>

> [@TheSynchronomicon](#):
>
> I want to be able to convert the following array from a text file
> 
> [[-7.5,-7.5,-5.0], [-2.5,-7.5,0.0], [2.5,-7.5,0.0], [7.5,-7.5,-5.0]], ,  
> [[-7.5,-2.5,-7.5], [-2.5,-2.5,5.0], [2.5,-2.5,5.0], [7.5,-2.5,-7.5]], ,  
> [[-7.5, 2.5, 0.0], [-2.5,2.5,-5.0], [2.5,2.5,-5.0], [7.5, 2.5, 0.0]], ,  
> [[-7.5, 7.5,-5.0], [-2.5,7.5,-10.00], [2.5,7.5,-10.00], [7.5,7.5,-5.0]]

That’s not even a 2d array, but a 3d 1 w/ dimensions 4x4x3:

```java
const DATA3D_4x4x3 = `
[[-7.5,-7.5,-5.0], [-2.5,-7.5,0.0], [2.5,-7.5,0.0], [7.5,-7.5,-5.0]], ,
[[-7.5,-2.5,-7.5], [-2.5,-2.5,5.0], [2.5,-2.5,5.0], [7.5,-2.5,-7.5]], ,
[[-7.5, 2.5, 0.0], [-2.5,2.5,-5.0], [2.5,2.5,-5.0], [7.5, 2.5, 0.0]], ,
[[-7.5, 7.5,-5.0], [-2.5,7.5,-10.00], [2.5,7.5,-10.00], [7.5,7.5,-5.0]]
`;

```

> [@TheSynchronomicon](#):
>
> Currently, I have split the array into the respective rows using the code below via double commas,

Indeed that’s the 1st step. But there’s a lot more to do in order to convert it to a 3d array!

After that 1st **split()** we only have a 1d array of strings so far:  
`const strArr1d = data.split(', ,');`

As for the 2nd step we can create a 2d array of strings by looping & splitting that 1d array of strings w/ delimiter `"],"`:

```auto
const delim = '],', len = strArr1d.length, strArr2d = Array(len).fill();
for (var i = 0; i < len; ++i) strArr2d[i] = strArr1d[i].split(delim);

```

Alternatively we can use Array.**from()** to do the same in 1 line:

```auto
const delim = '],', strArr2d = Array.from(strArr1d, str => str.split(delim));

```

> **[Array.from() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)**
>
> The Array.from() static method creates a new, shallow-copied Array instance from an iterable or array-like object.

Now that we have a 2d array of strings we need to upgrade it to a 3d array of floats.

That’s the most complicated part b/c we need to turn each string of the children arrays into an array of floats and assign it back to the them.

That way we’re adding a 3rd dimension to the 2d array, effectively converting it to a 3d array.

For that I’ve created a separate function named **create3dArrFrom2d()**:

```auto
function create3dArrFrom2d(arr2d) {
  const regexp = /[\[\]]/g, empty = '', delim = ',';

  const mapFn = str => new Float64Array(
    str.replace(regexp, empty).split(delim)
  );

  arr2d.forEach((arr1d, idx) => arr2d[idx] = arr1d.map(mapFn));

  return arr2d;
}

```

The function uses method String::**replace()** together w/ the [regex](https://developer.mozilla.org/en-US/docs/Glossary/Regular_expression) `/[\[\]]/g` in order to remove each `[` & `]` leftover characters from each string before we **split()** it:

> **[String.prototype.replace() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace)**
>
> The replace() method of String values returns a new string with one, some, or all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function called for each match. If...

In addition to that it iterates the outer array via method Array::**forEach()**:

> **[Array.prototype.forEach() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)**
>
> The forEach() method of Array instances executes a provided function once
> for each array element.

And then each inner array is transformed into a 2d array via Array::**map()**'s callback:

> **[Array.prototype.map() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)**
>
> The map() method of Array instances creates
> a new array populated with the results of calling a provided function on
> every element in the calling array.

And finally inside that callback we have Float64Array which converts the newly **split()** string array into a typed array of float values:

> **[Float64Array - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array)**
>
> The Float64Array typed array represents an array of 64-bit floating point numbers in the platform byte order. If control over byte order is needed, use DataView instead. The contents are initialized to 0 unless initialization data is explicitly...

Here’s the full code which can be copied & pasted into the browser’s console b/c it’s just vanilla JS:

## sketch.js:

```auto
/**
 * Array3D Split Parse (v1.0.0)
 * GoToLoop (2022/Mar/30)
 *
 * https://Discourse.Processing.org/t/
 * converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/4
 */

'use strict';

var FILENAME = 'data3d_4x4x3.txt', DATA3D_4x4x3 = `
[[-7.5,-7.5,-5.0], [-2.5,-7.5,0.0], [2.5,-7.5,0.0], [7.5,-7.5,-5.0]], ,
[[-7.5,-2.5,-7.5], [-2.5,-2.5,5.0], [2.5,-2.5,5.0], [7.5,-2.5,-7.5]], ,
[[-7.5, 2.5, 0.0], [-2.5,2.5,-5.0], [2.5,2.5,-5.0], [7.5, 2.5, 0.0]], ,
[[-7.5, 7.5,-5.0], [-2.5,7.5,-10.00], [2.5,7.5,-10.00], [7.5,7.5,-5.0]]
`;

var floatArr3d;

console.log(DATA3D_4x4x3);
handleFile({ name: FILENAME, data: DATA3D_4x4x3 });
console.table(floatArr3d);

function handleFile(file) {
  const { data, name } = file;
  if (typeof data != 'string') return console.error('Invalid file:', name);

  const strArr1d = data.split(', ,');
  console.table(strArr1d);

  const delim = '],', strArr2d = Array.from(strArr1d, str => str.split(delim));
  console.table(strArr2d);

  floatArr3d = create3dArrFrom2d(strArr2d);
}

function create3dArrFrom2d(arr2d) {
  const regexp = /[\[\]]/g, empty = '', delim = ',';

  const mapFn = str => new Float64Array(
    str.replace(regexp, empty).split(delim)
  );

  arr2d.forEach((arr1d, idx) => arr2d[idx] = arr1d.map(mapFn));

  return arr2d; // it's a 3d array now
}

```

You can use the following code to run it inside a browser:

## index.html:

```auto
<script async src=sketch.js></script>

```

And also batch files for both Node & Deno:

## node-run-sketch.bat:

```auto
node sketch.js
pause

```

## deno-run-sketch.bat

```auto
deno run sketch.js
pause

```

---

<div class="post-metadata">

### Author: ![pirela](https://avatars.discourse-cdn.com/v4/letter/p/3e96dc/32.png) [@pirela](https://discourse.processing.org/u/pirela)
#### Post date: [April 10, 2022, 7:11pm UTC](https://discourse.processing.org/t/converting-a-text-file-into-a-multidimensional-array-of-data-points/36007/5 "2022-04-10T19:11:15Z")

</div>

If you are at the origin of the file, it’s simpler to redact it as a **JSON standard :**

> let s =  
> `[[[-7.5,-7.5,-5.0], [-2.5,-7.5,0.0], [2.5,-7.5,0.0], [7.5,-7.5,-5.0]], [[-7.5,-2.5,-7.5], [-2.5,-2.5,5.0], [2.5,-2.5,5.0], [7.5,-2.5,-7.5]], [[-7.5, 2.5, 0.0], [-2.5,2.5,-5.0], [2.5,2.5,-5.0], [7.5, 2.5, 0.0]], [[-7.5, 7.5,-5.0], [-2.5,7.5,-10.00], [2.5,7.5,-10.00], [7.5,7.5,-5.0]] ]`

( just add a surrounding pair of brackets)  
**You just have to read it with parse** , and that’s all.

> let big = JSON.parse(s)  
> // access  
> console.log(big). // array(4) most external  
> console.log(big[0]) // array(4) a line  
> console.log(big[0][0]) // array(3) one vector  
> console.log(big[0][0][0]) // an element -7.5.

HTH
