Copy link to clipboard
Copied
Hi,
I am looking for a way of outputing a random word or group of words from a textfile.
I have gotten so far using $.evalFile expression to output text from a .txt file:
My .txt could look like this:
text1 = "word1";
text2 = "word2";
text3 = "word3";
text4 = ["word4", "word5", "a group of words", "another word"];
For text1-3 I can make a textlayers which are also called text1, text2, text3 and give them the Expression:
$.evalFile("/Users/rene/Desktop/AE\ TEST/sourcetext.txt");
eval(thisLayer.name)
For text4 I wrote:
r = Math.floor(random(3));
$.evalFile("/Users/rene/Desktop/AE\ TEST/sourcetext.txt");
eval(thisLayer.name)[r]
That works so far. But how can I write the Expression in a way, that it automatically recognizes the length of the text-array (= knowing how many elements there are to choose from) and put this into the random()?
thank you!
Try it this way:
$.evalFile("/Users/rene/Desktop/AE\ TEST/sourcetext.txt");
r = Math.floor(random(eval(thisLayer.name).length));
eval(thisLayer.name)[r]
Dan
Copy link to clipboard
Copied
Try it this way:
$.evalFile("/Users/rene/Desktop/AE\ TEST/sourcetext.txt");
r = Math.floor(random(eval(thisLayer.name).length));
eval(thisLayer.name)[r]
Dan
Copy link to clipboard
Copied
Hi Dan,
thanks a lot! That works perfectly!
Copy link to clipboard
Copied
Coding is 90% googleing:
https://www.google.com/search?client=firefox-b-d&q=getting+the+length+of+an+array
*Martin
Copy link to clipboard
Copied
Guess what:
Googled, got the first part done. Googled further (basically with the same search you wisely adivsed), didn't get it done.
Used my last 10% and used the forum....
Copy link to clipboard
Copied
Umm, okay.
Getting the length of an array is just such a basic task, that I was wondering how somebody how knows what an array is can't get this done.
I recommend stackoverflow (https://stackoverflow.com/) - that's a great ressource for coding questions. And usual people are posting code snippets so you can see how the function or method is applied and what the syntax have to look like. I always end up on stack overflow when I google my coding questions.
*Martin
Find more inspiration, events, and resources on the new Adobe Community
Explore Now