Having trouble with "Position" Command...

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Having trouble with "Position" Command...

Post by rjw524 » Wed Jan 13, 2016 4:02 pm

Hello,

I have a macro that copies text from a dynamically generated page to the clipboard. After copying the text, I try to use the "Position" command to find a particular "trigger" word.

If this word is present then that initiates a search for a second word whose position MUST be after that of the initial trigger word.

The positions of these words can change given the record displayed on the web page.

Here's an example:

It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way

Let's say the "Trigger Word" would be the 1st instance of "best" and that would kickoff the search for the FIRST instance of "times".

So, the position of best in this instance is 12
The position of times in this instance is 20

I can't get part 1 of the code to work properly. This code for simply finding the position of "best" returns 0.

Code: Select all

WaitClipBoard
wait 0.2
GetClipBoard>text
Separate>text,CRLF,lines
Let>ln=0
Repeat>ln
  Let>ln=ln+1
  Position>best,lines_%ln%,1,p
Until>ln=lines_count

MDL>%p%
Can anyone see what I'm doing wrong here?

Thanks,

RJ

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Having trouble with "Position" Command...

Post by JRL » Wed Jan 13, 2016 4:34 pm

Stepping through the code I can see that when LN = 1 the value of P is 12. If you want to evaluate via message, move your message to where it reports each line.

Code: Select all

WaitClipBoard
wait 0.2
GetClipBoard>text
Separate>text,CRLF,lines
Let>ln=0
Repeat>ln
  Let>ln=ln+1
  Position>best,lines_%ln%,1,p
  MDL>Line = %ln%%crlf%Position = %p%
Until>ln=lines_count

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Re: Having trouble with "Position" Command...

Post by Marcus Tettmar » Wed Jan 13, 2016 4:44 pm

Just do another Position search using the position of the trigger word as your starting point:

Code: Select all

Let>str=It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way
Let>trigger_word=best
Let>search_word=times
Position>trigger_word,str,1,pTrigger
If>pTrigger>0
  Position>search_word,str,pTrigger,pSearchWord
  Let>pSearchWord={%pSearchWord%+%pTrigger%-1}
  MessageModal>Position of "%search_word%" after "%trigger_word%" is: %pSearchWord%
Endif
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Having trouble with "Position" Command...

Post by rjw524 » Wed Jan 13, 2016 5:12 pm

Marcus Tettmar wrote:Just do another Position search using the position of the trigger word as your starting point:

Code: Select all

Let>str=It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way
Let>trigger_word=best
Let>search_word=times
Position>trigger_word,str,1,pTrigger
If>pTrigger>0
  Position>search_word,str,pTrigger,pSearchWord
  Let>pSearchWord={%pSearchWord%+%pTrigger%-1}
  MessageModal>Position of "%search_word%" after "%trigger_word%" is: %pSearchWord%
Endif
Ah...ok, this part of it was definitely me thinking it sounded FAR more difficult than it actually was. Thanks, Marcus.

rjw524
Pro Scripter
Posts: 104
Joined: Wed May 09, 2012 9:45 pm
Location: Michigan

Re: Having trouble with "Position" Command...

Post by rjw524 » Wed Jan 13, 2016 5:14 pm

Quick question...

Is it necessary to break the clipboard into a lined array?

Is there any way to just leave it as a block of text if that better suited one's purpose?

User avatar
JRL
Automation Wizard
Posts: 3532
Joined: Mon Jan 10, 2005 6:22 pm
Location: Iowa

Re: Having trouble with "Position" Command...

Post by JRL » Wed Jan 13, 2016 5:26 pm

From a PM I learned that the requirement is to find the first instance of a primary set of words, then find the first instance of a secondary set of words but only that instance that occurs after the first word from the primary set.
Is it necessary to break the clipboard into a lined array?
No in fact the following removes line feeds if they exist. Lowers the level of difficulty.

Try this:

Code: Select all

//You need your sets of words entered in some manner
//This script uses LabelToVar>
/*
FirstSet:
red
blue
yellow
green
white
*/

/*
SecondSet:
circle
square
triangle
rectangle
*/

//Delete this line in real use
PutClipboard>"Let me square this away for you guys. Michigan wears maize (a fancy word for yellow) and blue, and plays football in a stadium that looks like a huge circle with a rectangle inside it. Ohio State wears scarlet (a fancy word for red) and grey but Michigan State wears green and white and in basketball they use the triangle offense."


WaitClipBoard
wait 0.2
//GetClipboard
GetClipBoard>text
//Remove line breaks
StringReplace>text,crlf,,text

LabelToVar>FirstSet,FS
Separate>FS,crlf,First
Sub>First_Count,1

LabelToVar>SecondSet,SS
Separate>SS,crlf,Second
Sub>Second_Count,1

Length>text,LastFPos
Let>FS=0
Repeat>FS
  Add>FS,1
  Let>value=First_%FS%
  Position>value,text,1,FSPos
  If>{(%FSPos%<%LastFPos%)and(%FSPos%>0)}
    Let>FirstWord=value
    Let>LastFPos=FSPos
  EndIf
Until>FS=First_Count

Length>text,LastSPos
Let>SS=0
Repeat>SS
  Add>SS,1
  Let>Value=Second_%SS%
  Position>value,text,LastFPos,SSPos
  If>{(%SSPos%<%LastSPos%)and(%SSPos%>0)}
    Let>SecondWord=value
    Let>LastSPos=SSPos
  EndIf
Until>SS=Second_Count

MDL>First word from first set = %Firstword%%crlf%First word from second set following the first first set word = %SecondWord%

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts