Can't form array using splitText method (loadStrings)

I’m trying to get text elements from a local text file into an array.
pets.txt contains
cat
dog

Hopefully this code explains the problem. Thanks for looking!

var pets;
var petArray = [];

function preload() {
.
.
.

pets = loadStrings("pets.txt");
petArray = pets.splitText(',');

}

function draw() {
.
.
.
fill(0);
textSize(20);
text(pets,60,height-10); //prints… cat,dog
text(“X” + petArray[1],160,height-10); //prints… Xundefined
}

var pets;
var petArray = [];

function setup() {
  createCanvas(660,660);
  pets = "pet,cat,dog";
  petArray = split(pets, ',');
}

function draw() {
  fill(0);
  textSize(20);
  text(pets, 60, 130); //prints… cat,dog
  text( "X" + petArray[0], 60, 130 + 40);  //prints… Xundefined
}

Thanks for fast response Chrisir.
Using split(pets,’,’) in place of pets.splitText makes no difference. And yes predefining pets = “pet,cat,dog”; works but it’s using data from a text file that is the problem. In my application the text file contents will be changing and I don’t want to have to be hand-crafting js code every time.

You didn’t specify your problem precisely enough. Is the file there, does it load?

Of course my code is only an example. I just showed you a new split version. You want to load a text file.

Did you try with a new, blank text file? It could be that you need to use trim() or that you have white space in it.

Chrisir

It’s there, it loads, as demonstrated by
text(pets, 60, 130); //prints… cat,dog

Try index 0 instead?

index 0 gives same result.

ah, wait

your text file has each animal on a new line??

Then ',' won’t help you. This would help you when you had pets = "pet,cat,dog";.
My apologies.

Solution

pets should be an array already, one animal per slot.

please try
text("X" + pets [1],160,height-10);

Chrisir

For CSV files it’s much easier to deal w/ them via loadTable() rather than loadStrings():
p5js.org/reference/#/p5/loadTable
p5js.org/reference/#/p5.Table

Also we can’t do anything to a file while it’s still loading within preload():
p5js.org/reference/#/p5/preload

Await at least until setup() to access the file fully loaded:
p5js.org/reference/#/p5/setup

Chrisir - Yes!!!
But I don’t understand why the info was put into pets as an array. Is it because the items in the text file were on a new line?
This has been driving me mad. Many thanks

Many thanks for all that. I obviously need to home in more closely on comma separated strings and how to deal with them.
cheers

1 Like

my apologies, I was on the wrong track

That’s correct, loadStrings() reads the file and each line / paragraph is one slot in the array. That’s how loadStrings() works.

This file

cat
dog
mouse

gives you this array

cat
dog
mouse

(your case)

Another kind of file

When your file would be

cat,dog,mouse
apple,pea,tomato
house,car,horse

You would have an array and would for-loop over this array and use split() / splitText() command on every line to get an 2D array / grid.

But loadTable() is your friend.

Warm regards,

Chrisir

1 Like