Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

getting the length of an array from a Text-File?

Explorer ,
Nov 30, 2019 Nov 30, 2019

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!

 

TOPICS
Expressions , How to , Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 30, 2019 Nov 30, 2019

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

Translate
Community Expert ,
Nov 30, 2019 Nov 30, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 01, 2019 Dec 01, 2019

Hi Dan,

 

thanks a lot! That works perfectly!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Nov 30, 2019 Nov 30, 2019
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 01, 2019 Dec 01, 2019

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....

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Dec 01, 2019 Dec 01, 2019
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines