We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › turning text into a command
Page Index Toggle Pages: 1
turning text into a command (Read 526 times)
turning text into a command
Jul 29th, 2008, 3:45pm
 
Im new to this so its probable my question is a bit stupid.  I want to load a txt file and have processing break it down into individual characters and apply a command to each character so that each one runs a particular frame of an animation or block of code. Using the loadString() i have it reading a .txt but don't know how to break it up to apply the commands to the individual letters. I have it working with the keyPressed command but want it to run automatically. Thanks in advance.
Re: turning text into a command
Reply #1 - Jul 30th, 2008, 11:39am
 
There are several ways to do this.
A possible one is to split the string in an array of chars:

String s = "AxVrsT";

void setup()
{
 char[] chars = s.toCharArray();
 for (int i = 0; i < chars.length; i++)
 {
   print(chars[i] + " / ");
 }
}

A possibly simpler way is to use s.charAt(i) to get each char.

PS.: that's not really an integration question... Smiley
Re: turning text into a command
Reply #2 - Jul 31st, 2008, 6:36pm
 
Thanks PHilLho. The example offered works fine but it doesn't seem to offer my problem any solution.  Firstly i must make reference to the opening event in the functions via      String[] lines;    , so as not to cause an error when the sketch gets to the void setup().  In the setup i load the file with      lines = loadStrings("list.txt");      and have no problem printing to the console window this way.  I have used several versions of the solution offered but it will only add the '/' at the end of the printed line, i cannot make reference to any part of the text file or break it up into seperate characters as to make reference later, as a series of errors takes place. It wont even allow me to turn the letters toLowerCase(). Any suggestions are greatly appreciated.
Re: turning text into a command
Reply #3 - Jul 31st, 2008, 7:51pm
 
Use PhiLho's tip and the switch method :

Quote:
String s = "abc";
for (int i = 0; i < s.length(); i++) {
 char c = s.charAt(i);
 switch(c) {
 case 'a' :
   println("-> a");
   break; // don't forget to break
 case 'b' :
   println("-> b");
   break; // don't forget to break
 case 'c' :
   println("-> c");
   break; // don't forget to break
 }
}
Re: turning text into a command
Reply #4 - Aug 1st, 2008, 4:28pm
 
Thanks antiplastik, i realise what i was doing wrong now, i was trying to run the string within the setup and then reference it within the draw which was giving me all my errors, i just put everything but the string itself within draw and then apply whatever block of animation i want to it. Much appreciated for both your help, that one had me stuck for weeks. Thanks again.
Re: turning text into a command
Reply #5 - Aug 5th, 2008, 6:11pm
 
O.k., i have followed the instructions offered by antiplastik and below is the code i have come up with.  The problem i am having is that the void draw command is only reading the first and last set of drawing commands, i am aware the void draw() will only update after it has finished reading the block but i need it to update the visual for every character it meets, does any kind wise programmer have any suggestions for what i should do with the attached code to make the image refresh for every character it reads, i presume an alternative structure will be required but anything i have tried thus far has failed to improve it. Please and thanks.

String s = "cab";


void setup(){
 size (300, 300); // size of sketch
 s = s.toLowerCase(); // change string to lower case
 frameRate(0); // framerate
}

void draw(){
 println(frameCount); // prints frame number at very start
 for (int i = 0; i < s.length(); i++) { // reads string and breaks it up into characters
 
 char ch = s.charAt(i); // position of each Character in the String
 switch(ch) {  // acknowledge the character instead of the char function

 case 'a':
 println("-> a");
 fill (240, 15, 15); // red
 rect (0,0, width, height);
 break;
 


 case 'b' :
 println("-> b");
 fill (82, 193, 35); // blue
 rect (0,0, width, height);
 break;


 case 'c' :
 println("-> c");
 fill (18, 59, 178); // green
 rect (0,0, width, height);
 break;
 
 }
 
 delay(2000); // slows programme here for two seconds
 frameCount ++; // starts next frame
 println(frameCount); // prints frame number between characters

 }
 
 noLoop();  
 println("--------------------> End");
}








Re: turning text into a command
Reply #6 - Aug 5th, 2008, 6:14pm
 
What you need it:

Code:

int i;
String s="cab";

void setup()
{
//... stuff
i=0;
}

void draw()
{
char ch=s.charAt(i);
switch(ch)
{
// ...
}
delay(1000);
i++;
}


This way you will only use one character at a time.
Re: turning text into a command
Reply #7 - Aug 5th, 2008, 6:39pm
 
Thanks a lot JohnG, totally sorted me out.
Re: turning text into a command
Reply #8 - Aug 5th, 2008, 6:52pm
 
This might be a bit late to mention, but have you looked at L-Systems at all?

The reference doesn't really cover it, but it provides a nice handy ruleset for guiding a drawing program that you can even turn into a fractal.

I'm sure it's been covered on the forum more than once.
Page Index Toggle Pages: 1