How to import a txt file and then insert the string directly into a function?

edited December 2017 in How To...

// the draw.txt document contains the following: "rect(30, 20, 55, 55);"

void draw() { draw.txt; }

Tagged:

Answers

  • edited December 2017

    not so easy

    best store the numbers separated by commas without rect, commas or ( ) ;

    just

    30, 20, 55, 55

    look at loadstrings and split in the reference

  • Thanks for your response Chrisir! I'm still hoping there's a way to load a string from which to write commands in addition to insert variables.

  • Do your files contain Processing commands?

    Kf

  • Yes, my .txt file contains a Processing command.

  • edited December 2017 Answer ✓

    Sounds like you are interested in code evaluation during program execution. See past discussions, e.g.

    @GoToLoop sometimes recommends nashhorn.

    You can also just parse your data and execute. So if the name in column 1 is circle, line, rect, pass the variables to circle, line, rect with e.g. a switch statement.

  • Thanks for your advice Jeremey. I'll follow-up on the links and the process you suggested. Overall, I'm grateful for the support of the community within this forum. G

  • edited December 2017 Answer ✓

    Good luck, @garystil.

    To expand on the non-code solution, it looks something like this:

    • for each line in data
      • switch on the keyword
        • if the keyword is "rect", pass args 2,3,4,5... to rect
        • if the keyword is "ellipse" pass the args to ellipse... etc.

    If you are doing something simple, this is pretty easy -- much easier than code evaluation. If you are trying to push and pop styles, specify loops in your data, etc. then this can get really complicated really quickly -- and then it is much easier to implement a code evaluation solution and focus on programming in your data rather than inventing your own language / parser.

Sign In or Register to comment.