Welcome Dialog

Welcome to the Community!

We have a brand new look! Take a tour with us and explore the latest updates on Adobe Support Community.


Array with running total instead of individual values

Participant ,
May 19, 2021 May 19, 2021

Copy link to clipboard

Copied

Hi all,

I'm trying to create a script to spit out a running total of the number of words per line of text. I can get it to spit out the count for each individual line, but what I readlly want is it to be a total for all words up to that point. Here's where I'm at:

 

for (var i = 0; i < myText.lines.length; i++) {
 myCounts[i] = [app.selection[0].lines[i].words.length]
}

 

The array myCounts contains the number of words in each line, but is there some way I can rewrite this so each item in the array is a sum of all previous items? I'm thinking something like counting "lines 0 through i" instead of just "line i". Or do I need to create a second array, maybe?

Thanks for any advice!

TOPICS
Scripting

Views

147

Likes

Translate

Translate

Report

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

Adobe Community Professional , May 19, 2021 May 19, 2021
Where's the rest of the code? myText is undefined at this stage, as is myCounts, and then you're referencing the selection instead of myText lines. Quite confused. Here's what I think you're going for. // var myText = app.selection[0]; var numLines = myText.lines.length; var myCounts = new Array(numLines); for (var i = 0; i < numLines; i++) { if (i == 0) { myCounts[i] = myText.lines[i].words.length; } else { myCounts[i] = myText.lines[i].words.length + myCounts[i-1]; } } ...

Likes

Translate

Translate
Adobe Community Professional ,
May 19, 2021 May 19, 2021

Copy link to clipboard

Copied

Where's the rest of the code? myText is undefined at this stage, as is myCounts, and then you're referencing the selection instead of myText lines. Quite confused. Here's what I think you're going for.

//
var myText = app.selection[0];
var numLines = myText.lines.length;
var myCounts = new Array(numLines);
for (var i = 0; i < numLines; i++) {
   if (i == 0) { 
      myCounts[i] = myText.lines[i].words.length;
   } else { 
       myCounts[i] = myText.lines[i].words.length + myCounts[i-1];
   }
}

 

Likes

Translate

Translate

Report

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
Participant ,
May 19, 2021 May 19, 2021

Copy link to clipboard

Copied

There's a bunch more to what I'm trying to do with this script so I only included the part I got hung up on.

I got it to work with a second array, but really it was the if (i == 0) bit that was tricking me! Now it seems so obvious 😕

 

Thank you!

 

Likes

Translate

Translate

Report

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
Adobe Community Professional ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

LATEST

Hi,

 

(allowing for the comments already made, which may mean my answer is not as good as it could be).

You want myCounts to be an ever increasing total of the number of words per line:

for example :

Line one has five words

Line two has five words

Line three has five words

 

You myCounts array would have the values : [ 5, 10, 15];

 

if that is correct then you can do something like this:

var myCounts = [];
for ( var i = 0; i < app.selection[0].lines.length; i++){
    var j = (i > 0) ? i -1 : i;
    if ( myCounts.length > 0){
        myCounts.push ( myCounts[j] + app.selection[0].lines[i].words.length);
    } else {
        myCounts.push ( app.selection[0].lines[i].words.length)
    }
}

Likes

Translate

Translate

Report

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