StudioProjects
StudioProjects

Reputation: 139

P5 array intersection

I'm trying to extract the values from one array from the index of another. I'm loading two text files using loadStrings, which is working fine. I'm loading that data into two separate arrays. The two text files are different lengths, the first contains three values - a line number and then an x and y coordinate. The second text file contains the line numbers of the data that I want to extract from the first text file. With only two months of coding behind, me this is an extremely daunting task that has left me completely stymied! My (very basic) code thus far is as follows:

let data1;
let data2;
let parsedData1;
let parsedData2;
let combinedData;

function preload() {
  data1 = loadStrings('assets/data1.txt'); // load the data1 file
  data2 = loadStrings('assets/data2.txt'); // load the data2 file
  // console.log(data1);
  // console.log(data2);
}

function setup() {
  createCanvas(600, 400);   
}

function draw() {
  noLoop();
  background(0);
  
  readData1()
  readData2()
}

function readData1() {
  parsedData1 = new Array(data1.length); // create an array for the parsed data

  for (let i = 0; i < data1.length; i++) {
    let parsedData1 = splitTokens(data1[i]);
    // console.log(data1); 
  }
}

function readData2() {
  let combinedData = concat(parsedData1, data2);
  // console.log(combinedData);
}

The text files that I'm loading contain the following:

text1:

1 200 50
2 100 25
3 200 63
4 123 456
5 124 200
6 700 500
7 600 500
8 200 121
9 300 100

text2:

3
5
8

The result that I'm seeking is:

3 150 100
5 124 200
8 200 121

Apparently the desired result can be achieved using loops, but I've yet to work out how!

Upvotes: 4

Views: 156

Answers (2)

Dietrich
Dietrich

Reputation: 681

Since you mentioned loops, here's how to do it with loops:

If you know that the "index numbers" in data1.txt don't skip any number:

for(let i = 0; i < data2.length; i++) {
  let index = data2[i];
  console.log(data1[index]);
}

Otherwise, you can use nested loops like this:

for(let i = 0; i < data2.length; i++) {
  let index = data2[i];
  for(let j = 0; j < data1.length; j++)
  {
    let line = data1[j];
    let lineArr = line.split(" ");
    if(lineArr[0] == index) {
      console.log(line);
    }
  }
}

Upvotes: 1

ATD
ATD

Reputation: 1364

In order to compare the two strings, they need to be in arrays (or something similar).

Have a look at:

  let text1 = document.getElementById("originaltext").innerHTML.trimRight();
  let compare1 = document.getElementById("comparevalues").innerHTML.trimRight();
  let a1 = text1.split("\n");
  console.log(a1);
  let c1 = compare1.split("\n");
  console.log(c1);
  const r1 = a1.filter(a => c1.includes(a.split(" ")[0]));
  console.log(r1);
<pre id="originaltext">
1 200 50
2 100 25
3 200 63
4 123 456
5 124 200
6 700 500
7 600 500
8 200 121
9 300 100
</pre>
<pre id="comparevalues">
3
5
8
</pre>

This reads the strings from PRE tags in the example (but would be from your original text strings) and converts them into arrays. Using "includes" filters the first list if the first number appears in the second list and returns a new array of the results

Upvotes: 1

Related Questions