Skip to main content
Jo_2013
Known Participant
April 10, 2017
Question

Find specific words within an array of words

  • April 10, 2017
  • 2 replies
  • 809 views

Following script will report all words on the page to the console with a space between each word

var i, j, ckWord, numWords, aWords = [];

for (i = 0; i < this.numPages; i++ ) {

numWords = this.getPageNumWords(i); for (j = 0; j < numWords; j++) {

ckWord = this.getPageNthWord(i, j)

if (ckWord) {

aWords.push(ckWord);  // Add word to array

}

}

var m = aWords.join(" ");

console.println("Words found are " + m);

}

The following script is a variation of the above script to find words within the array of words reported to the console.

It is not working though, the number 730 is reported back to the console not the searched for words an example being  "SERVICES PLAN SHEET" .

Can anyone please provide assistance to modify the script to find words within an array of words.

var i, j, ckWord, numWords, aWords = [];

for (i = 0; i < this.numPages; i++ ) {

numWords = this.getPageNumWords(i); for (j = 0; j < numWords; j++) {

ckWord = this.getPageNthWord(i, j)

if (ckWord) {

aWords.push(ckWord);  // Add word to array

}

}

var m = aWords.join(" ");

if (m == "SERVICES PLAN SHEET")

{

console.println(m);

}

}

730 (this is what is reported to the console)

Many thanks.

This topic has been closed for replies.

2 replies

Inspiring
April 10, 2017

You should be aware that the order of the words is not the reading order of the words on the page but the order in which the words were added to the page. So if the page's text is edited with Acrobat then the order of the words will be changed.

Jo_2013
Jo_2013Author
Known Participant
April 11, 2017

Thanks for your reply. Below is the console print out for all of the words. The words I need extracted as an example are highlighted (they are in the correct order). Can you please advise if it is possible to amend the script to find the sub words within all of the words found?

Console print out for all words

Words found are : CONVEYOR RECLAIMER 1 B C 2 3 FHR ELEVATION TAIL END 25V LEGEND SERVICE WATER WS UNDERGROUND SERVICE WATER FIRE HOSE REEL SERVICES PLAN SHEET ISOLATION VALVE WATER CONTROL SPRAY NOZZLE SPRAY BAR FIRE HYDRANT SERVICE WATER

Many thanks for your assistance.

Bernd Alheit
Community Expert
Community Expert
April 11, 2017

Use the indexOf() method:

JavaScript String indexOf() Method

Legend
April 10, 2017

Your code as written seems as if it should print nothing unless the page has exactly three words, which are SERVICES PLAN SHEET in that order. In this case it should print SERVICES PLAN SHEET. If it has any extra words, a different order or anything else it should print nothing. Maybe the 730 is coming from somewhere else. Maybe you intend the code to do something different.