Is it possible to make my random string appear all but only one time each, during the run ?

edited March 2017 in How To...

I have random texte that appears but I would like each sentence (string) to appear only one time each, during the running of the sketch. Also to go to the line I use \n is there any way to constrain my string to do it alone ? Thanks you very much !

Answers

  • Can you post the code you have so far? It will be useful to see your approach.

    Do you have your strings stored in an array?

    This next post does not answer your question but it shows one possible idea: https://forum.processing.org/two/discussion/comment/90020/#Comment_90020

    Kf

  • @alicessing --

    If you put your strings in a StringList then you can use remove to take out each randomly chosen string after it is used.

  • Thanks ! I am going to look at your suggestion. I have something like 100 sentences I don't think I can do it by using String list or I will have to create an inventory.append("") for each of my sentences ? Here is my code :

    import processing.pdf.*;
    
    PFont typo;
    PImage paper;
    String titre = "Manifeste"; 
    
    
    String [] phrase1 = { 
    "Tranchons-en : le merveilleux est toujours beau, \nn'importe quel merveilleux est beau, il n'y a même que le merveilleux qui soit beau.","Aucun critère technologique n'est nécessaire, si ce n'est que l'oeuvre doit exister sur un ou plusieurs ordinateurs.","L’homme doit garder ses racines, savoir d’où il est.","Il faudrait redonner un sens à la vie communautaire.","Il y a un travail urgent à fournir : recueillir les fauves ou récits de veillée, les chansons populaires, les coutumes…","Le passé doit seulement constituer un enseignement."
    };
    
    float x, y;
    
    
    void setup() {
      size(827, 1170, PDF, "manifeste.pdf");
     // size(2480,3508); 850 500
      smooth();
      frameRate(1);
      background(255);
      fill(50);     //! 
    //typo = createFont("Cambria", 10);
      typo = createFont("Cambria-48", 10);  
      textFont(typo, 12);
     // textFont(typo, 15);
     // text(titre,10,50);
     }
    
    void draw() {
    
    
      background(255);
    
      y = y + 110;
     text(phrase1[int(random(0, phrase1.length))], 50, 50);
     text(phrase1[int(random(0, phrase1.length))], 50, 150);
     text(phrase1[int(random(0, phrase1.length))], 50, 250);
     text(phrase1[int(random(0, phrase1.length))], 50, 350);
     text(phrase1[int(random(0, phrase1.length))], 50, 450);
     text(phrase1[int(random(0, phrase1.length))], 50, 550);
     text(phrase1[int(random(0, phrase1.length))], 50, 650);
     text(phrase1[int(random(0, phrase1.length))], 50, 750);
     text(phrase1[int(random(0, phrase1.length))], 50, 850);
     text(phrase1[int(random(0, phrase1.length))], 50, 950);
     text(phrase1[int(random(0, phrase1.length))], 50, 1050);
    
    
    PGraphicsPDF pdf = (PGraphicsPDF) g;  
     pdf.nextPage();  
    
     if (frameCount ==5) {   
       exit();    
    
    }
    
  • instead of \n look at text () with 5 parameters which is text in a box so to speak

    You could shuffle the list and then bring in a counter that just goes through it with a simple ++

  • edited March 2017 Answer ✓

    @alicessing --

    Edit: You can create a new StringList directly from a String[] array (see GoToLoops answer below).

    String[] phrase1 = {"a","b","c","d"};
    StringList phraselist = new StringList(phrase1);
    

    You can also load strings from an array into an existing StringList with an enhanced for loop:

    ...taking the form:

    for (datatype element : array) { 
      statements
    }
    

    Like this:

    // load string array into existing StringList
    StringList phraselist = new StringList();
    // ....
    String[] phrase1 = {"a","b","c","d"};
    for (String element : phrase1) {
      phraselist.append(element);
    }
    

    Once you have a StringList it can be shuffled using its shuffle() method:

    ...like this:

    // shuffle
    phraselist.shuffle();
    println(phraselist);
    

    Output:

    StringList size=4 [ "c", "d", "b", "a" ]

    For very long lists of strings, consider saving them in a text file and loading them using loadStrings:

    ...like this:

    String[] phrase1 = loadStrings("phrase1.txt");
    
  • your big problem is drawing everything in the same iteration of draw().

    the screen is only updated at the end of each call to draw().

    https://forum.processing.org/two/discussion/8085/i-display-images-in-sequence-but-i-see-only-the-last-one-why

  • edited March 2017 Answer ✓
    // forum.Processing.org/two/discussion/21425/
    // is-it-possible-to-make-my-random-string-appear-all-
    // but-only-one-time-each-during-the-run#Item_7
    
    // 2017-Mar-16
    
    final String[] phraseArray = {
      "Il n'y a même que le merveilleux qui soit beau", 
      "Aucun critère technologique n'est nécessaire,", 
      "Si ce n'est que l'œuvre doit exister sur un ou plusieurs ordinateurs.", 
      "L'homme doit garder ses racines,", 
      "Il faudrait redonner un sens à la vie communautaire."
    };
    
    printArray(phraseArray);
    println();
    
    final StringList phraseList = new StringList(phraseArray);
    phraseList.shuffle();
    println(phraseList, ENTER);
    
    phraseList.array(phraseArray);
    printArray(phraseArray);
    
    exit();
    
  • Answer ✓

    There is also the split and splitTokens functions. If you write your phrases and they are separated by a special character (for example... your phrases enclosed in quote remarks) then you can use split like this:

    String modified_phrase1="\"phrase1\"\"phrase2\"";
    println("ORIGINAL PHRASES: "+modified_phrase1);
    
    String[] phrases = split(modified_phrase1, "\"");
    println("PHRASES FOUND USING SPLIT"+phrases.length);
    for(int i=0;i<phrases.length;i++)
    println(i+" "+phrases[i]);
    
    
    String[] phrases2 = splitTokens(modified_phrase1, "\"");
    println("PHRASES FOUND USING SPLITTOKEN"+phrases2.length);
    for(int i=0;i<phrases2.length;i++)
    println(i+" "+phrases2[i]);
    

    The main difference is that split will produce empty Strings between contiguous delimiters while splitTokens doesn't. It treats contiguous delimiters together

    Kf

  • Thanks very much everybody and thanks for your explanations this is not exactly what I wanted to do but finaly I think it is a better thing like this and it going to be easier for me for what I want to do then. And I will keep to try to do the other things by myself :)

  • import processing.pdf.*;
    
    PFont typo;
    PImage paper;
    String titre = "Manifeste"; 
    
    
    String [] phrase1 = { 
      "Tranchons-en : le merveilleux est toujours beau, \nn'importe quel merveilleux est beau, il n'y a même que le merveilleux qui soit beau.", 
      "Aucun critère technologique n'est nécessaire, si ce n'est que l'oeuvre doit exister sur un ou plusieurs ordinateurs.", 
      "L’homme doit garder ses racines, savoir d’où il est.", "Il faudrait redonner un sens à la vie communautaire.", 
      "Il y a un travail urgent à fournir : recueillir les fauves ou récits de veillée, les chansons populaires, les coutumes…", 
      "Le passé doit seulement constituer un enseignement."
    };
    
    float x, y=33;
    
    int phraseCounter;
    
    PGraphicsPDF   pdf = (PGraphicsPDF) g;
    
    void setup() {
    
      // size(827, 1170, PDF, "manifeste.pdf");
      // size(2480,3508); 850 500
      size(1280, 908); // size(2480,3508); 850 500 
    
      smooth();
      frameRate(1);
      background(255);
    
    
    
      fill(50);     //! 
      //typo = createFont("Cambria", 10);
      typo = createFont("Cambria-48", 10);  
      textFont(typo, 12);
      // textFont(typo, 15);
      // text(titre,10,50);
    
    
      // we shuffle phrase1 !!!!!!!!!!
      final StringList phraseList = new StringList(phrase1);
      phraseList.shuffle();
      // println(phraseList, ENTER);
    
      //phrase1
      phraseList.array(phrase1);
      printArray(phrase1);
    
      println ("Press any key to go to next page");
    }
    
    void draw() {
    
      background(255);
    
      text(phrase1[phraseCounter], 
        50, y, 
        120, 700);
    
      //pdf.nextPage();  
    
      //if (frameCount ==5) {   
      //  exit();
      //}
      //
    }
    
    void keyPressed() {
    
      if (phraseCounter>=phrase1.length-1)
        return; // leave here  
    
      phraseCounter++;
      y = y + 110;
    
      //  pdf.nextPage();
    }
    //
    
Sign In or Register to comment.