Unable to split a text file into an array in P5

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

  • 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.

    var data;
    var value = [] ;
    var data2;
    var value2 = [];
    
    function preload() {
      data = ("rock, paper, scissor");
      data2 = loadStrings('text.txt');
    }
    
    function setup() {
      print("data = ", data); // prints "data = rock, paper, scissor"
      print("data2 = ", data2); // prints "data2 = ["rock, paper, scissor"]"
    
      value = split(data, ",");
      value2 = split(data2, ","); // if slashes removed nothing that follows runs
    
      print("value[0] = ", value[0]); // prints "value[0] = rock"
      print("value[1] = ", value[1]); // prints "value[1] = paper"
      print("value[2] = ", value[2]); // prints "value[2] = scissor"
      // print ("value2[0] = ", value2[0]); // nothing prints when slashes removed
    }
    
  • ... but am still unable to convert the loaded String...

    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."

    var data;
    var value = [] ;
    var data2;
    var value2 = [];
    
    function preload() {
    data = ("rock, paper, scissor");
    data2 = loadStrings('text.txt'); // content of text.txt file is "rock, paper, scissor"
    }
    
    function setup() {
    print("data = ", data); // prints "data = rock, paper, scissor"
    print("data2 = ", data2); // prints "data2 = ["rock, paper, scissor"]"
    
    value = split(data, ",");
    value2 = split(data2, ","); // if slashes removed nothing that follows runs
    
    print("value[0] = ", value[0]); // prints "value[0] = rock"
    print("value[1] = ", value[1]); // prints "value[1] = paper"
    print("value[2] = ", value[2]); // prints "value[2] = scissor"
    // print ("value2[0] = ", value2[0]); // nothing prints when slashes removed
    }
    
  • You've reposted your sketch instead of "text.txt"! :-&

  • // content of text.txt file is "rock, paper, scissor"

  • Answer ✓

    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"

    var data;
    var data2;
    var value = [];
    var value2 = [];
    
    function preload() {
      data = ("rock, paper, scissor");
      data2 = loadStrings('text.txt');
    }
    
    function setup() {
      print("data = ", data); // prints "data = rock, paper, scissor"
      print("data2 = ", data2); // prints "data2 = ["rock, paper, scissor"]"
    
      value = split(data, ",");
      //value2 = split(data2, ","); // if slashes removed nothing that follows runs
    
      print("value[0] = ", value[0]); // prints "value[0] = rock"
      print("value[1] = ", value[1]); // prints "value[1] = paper"
      print("value[2] = ", value[2]); // prints "value[2] = scissor"
      print ("data2[0] = ", data2[0]); // prints "data2[0] = rock, paper, scissor"
    }
    
  • edited December 2017 Answer ✓

    note: text.txt file is "rock, paper, scissor"

    Make it so that each word is its own line like this:

    "text.txt":

    rock
    paper
    scissor
    

    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 :-)

Sign In or Register to comment.