geo3geo
February 26, 2021, 8:52am
1
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
}
Chrisir
February 26, 2021, 9:34am
2
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
}
geo3geo
February 26, 2021, 9:57am
3
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.
Chrisir
February 26, 2021, 11:13am
4
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
geo3geo
February 26, 2021, 12:00pm
5
It’s there, it loads, as demonstrated by
text(pets, 60, 130); //prints… cat,dog
geo3geo
February 26, 2021, 3:36pm
7
index 0 gives same result.
Chrisir
February 26, 2021, 4:21pm
8
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
A comma-separated values (CSV) file is a delimited text file that uses a comma to separate values. Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format. A CSV file typically stores tabular data (numbers and text) in plain text, in which case each line will have the same number of fields.
The CSV file format is not fully standardized. The basic idea of separ...
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
geo3geo
February 26, 2021, 5:19pm
10
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
geo3geo
February 26, 2021, 5:38pm
11
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
Chrisir
February 26, 2021, 7:20pm
12
my apologies, I was on the wrong track
Chrisir
February 26, 2021, 7:41pm
13
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