We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Using P5, both on- and offline, I'm unable to split a loaded .txt file into an array.
The loaded .txt file can be printed in the console before it has been split but not after. However, if the string originates within the program ("rock, paper, scissor"), it can be printed both before and after splitting.
Would be grateful for advice. Thanks in advance. G
var data; var value = []; var data2; var value2 = [];
function preload() { data = ("rock, paper, scissor"); data2 = loadStrings('text.txt'); }
function setup() { print("data = ", data); print("data2 = ", data2);
value = split(data, ","); //value = split(data2, ",");
print("value[0] = ", value[0]); print("value[1] = ", value[1]); print("value[2] = ", value[2]); //print ("value[0] = ", value2[0]); })
Answers
https://Forum.Processing.org/two/discussion/15473/readme-how-to-format-code-and-text
https://p5js.org/reference/#/p5/loadTable
I apologize for posting code inappropriately and have corrected it below.
Thanks GoToLoop for your response. However, I'm not attempting to load a table, just a string of words. Have tried multiple configurations but am still unable to convert the loaded String (data2) into an array (value2). Would be grateful for your second look and advice.
You should post the contents (or at least a sample) of your "text.txt" file.
This way, it's easier to find out which loading function is the ideal for it. ;)
Thanks GoToLoop. I've reposted the correctly formatted code noting the content of the text.txt file per your comment.
Though the file appears to load without a problem, I'm still unable to split it into an array named "value2."
You've reposted your sketch instead of "text.txt"! :-&
loadStrings is doing the right thing. The problem is that your file only contains one string...
Hi koogs and GoToLoop.
Yes, my text file is a single string ("rock, paper, scissors") and, after loading, it appears to result in a single value within the array so that value2[0] = rock, paper, scissor.
I've searched through multiple on- and off-line references and have been unable to find the procedure to split a loaded text file. I sense the solution is quite simple but it's in my blind spot. Your guidance is much appreciated.
note: text.txt file is "rock, paper, scissor"
Make it so that each word is its own line like this:
"text.txt":
This way, when it's loaded via loadStrings(), each index of the resulting array is simply 1 word. :D
Thanks GoToLoop & koogs for your feedback and advice. I just viewed one of Dan Shiffman's videos in which he loaded the full text of Hamlet (multiple lines), joined it with a blank space, and then split it into multiple values. I followed that path by adding a word on the second line of my txt file, joining that file with a blank space, and then splitting it. Success :-)