Processing Practice #02 | Text Processing

Contents

  1. Functions
  2. Import
  3. Export
  4. Practice

Functions

  •  <text>charAt(<n>)  –  find<n>th character in <text>
  • <text>.length  –  find the length of <text>
  • split(<text to split>,<separator>)  –  split <text to split> with <separator>
  • join(<list of texts>,<separator>)  –  join <list of texts> with <separator>
  • <text to search>.indexOf(<character>)  –  find <character> from <text to search> It returns index of character, if it’s not found, it returns “-1”
  • match(<text>, <header>+”{.*?}”+<footer>)  –  find sub texts between <header> and <footer> in <text>
  • trim(<text>)  –  delete whitespace in <text>

Import

You can import txt file with following code:

String[] strings = loadStrings("textfile.txt");

Export

You can export txt file with following code:

saveStrings("textfile.txt", strings);

Practice

Send/Receive multiple points

import oscP5.*;
import netP5.*;

OscP5 osc;
NetAddress location;
ArrayList<PVector> mp, yp;

void setup() {
  size(500, 500);
  frameRate(25);
  //strokeWeight(10);
  osc = new OscP5(this, 12007);
  location = new NetAddress("127.0.0.1", 12000);
  mp = new ArrayList<PVector>();
  yp = new ArrayList<PVector>();
}

void draw() {
  background(127);
  stroke(0);
  fill(0, 100);
  beginShape();
  for (int i=0; i<mp.size (); i++) {
    ellipse(mp.get(i).x, mp.get(i).y, 10, 10);
    vertex(mp.get(i).x, mp.get(i).y);
  }
  endShape();
  stroke(255);
  fill(255, 100);
  beginShape();
  for (int i=0; i<yp.size (); i++) {
    ellipse(yp.get(i).x, yp.get(i).y, 10, 10);
    vertex(yp.get(i).x, yp.get(i).y);
  }
  endShape();
  OscMessage m = new OscMessage("/pos");
  String[] poses = new String[mp.size()];
  for (int i=0; i<mp.size (); i++) {
    poses[i] = str(mp.get(i).x)+","+str(mp.get(i).y);
  }
  m.add(join(poses,"/"));
  osc.send(m, location);
}

void mousePressed() {
  if (mouseButton == CENTER) {
    mp.add(new PVector(mouseX, mouseY));
  }
}
void keyPressed() {
  if (key == 'd') {
    mp = new ArrayList<PVector>();
  }
}

void oscEvent(OscMessage m) {
  print(" addrpattern: "+m.addrPattern());
  println(" typetag: "+m.typetag());
  if (m.checkAddrPattern("/pos")==true) {
    String message = m.get(0).stringValue();
    String[] messages = message.split("/");

    yp = new ArrayList<PVector>();

    for (int i=0; i<messages.length; i++) {
      String[] pos = messages[i].split(",");
      yp.add(new PVector(int(pos[0]), int(pos[1])));
    }
  }
}