# Can't form array using splitText method (loadStrings)

**URL:** <https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092>\
**Category:** Beginners\
**Created:** [February 26, 2021, 8:52am UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092 "2021-02-26T08:52:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 8:52am UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/1 "2021-02-26T08:52:06Z")

</div>

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  
}

---

<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:** [February 26, 2021, 9:34am UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/2 "2021-02-26T09:34:35Z")

</div>

```auto
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
}

```

---

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 9:57am UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/3 "2021-02-26T09:57:01Z")

</div>

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.

---

<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:** [February 26, 2021, 11:13am UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/4 "2021-02-26T11:13:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 12:00pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/5 "2021-02-26T12:00:42Z")

</div>

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

---

<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:** [February 26, 2021, 3:35pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/6 "2021-02-26T15:35:24Z")

</div>

> [@geo3geo](#):
>
> text(“X” + petArray[1],

Try index 0 instead?

---

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 3:36pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/7 "2021-02-26T15:36:20Z")

</div>

index 0 gives same result.

---

<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:** [February 26, 2021, 4:21pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/8 "2021-02-26T16:21:01Z")

</div>

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

---

<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:** [February 26, 2021, 4:27pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/9 "2021-02-26T16:27:21Z")

</div>

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

> **[Comma-separated values](https://en.wikipedia.org/wiki/Comma-separated_values)**
>
> 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](http://p5js.org/reference/#/p5/preload)

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

---

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 5:19pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/10 "2021-02-26T17:19:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![geo3geo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/geo3geo/32/11881_2.png) [@geo3geo](https://discourse.processing.org/u/geo3geo)\
**Post date:** [February 26, 2021, 5:38pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/11 "2021-02-26T17:38:58Z")

</div>

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

---

<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:** [February 26, 2021, 7:20pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/12 "2021-02-26T19:20:51Z")

</div>

my apologies, I was on the wrong track

---

<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:** [February 26, 2021, 7:41pm UTC](https://discourse.processing.org/t/cant-form-array-using-splittext-method-loadstrings/28092/13 "2021-02-26T19:41:32Z")

</div>

> [@geo3geo](#):
>
> 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?

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

This file

```auto
cat
dog
mouse

```

gives you this array

cat  
dog  
mouse

(your case)

**Another kind of file**

When your file would be

```auto
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
