AutoHotkey Community

It is currently May 27th, 2012, 12:51 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: StringGetPos
PostPosted: December 26th, 2009, 10:56 pm 
Offline

Joined: July 28th, 2009, 6:09 pm
Posts: 69
I really needed a command to search through a block of text (which I would have used 'WinGetText' to get) and find a specific phrase in this block of text and then tell me the line that it is in, however, after fiddling around with 'StringGetPos' all I get is the NUMBER OF LETTERS rather than the number of lines that it is down. How can I fix it?

For instance, say that notepad has this text in it:

--------------------
Hello
Goodbye
How are you

What is your name?
What is your address?
534i92
text
more stuff
and so on...
--------------------

Say, for instance I have tonnes of these but in each one the 'what is your name' is in a different line? I need it to search the string and tell me exactly what line 'What is your name?' in.

Please not RegEx! I fail miserably at that :D

Thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 26th, 2009, 11:04 pm 
Code:
Line := 1

Loop
     { Filereadline, Text, File.txt, %Line%
     If Text = What is your name?
          MsgBox, %Line%
     Line++
     Return
          }


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: December 26th, 2009, 11:59 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
If you have many texts (files or variables) it is easiest if you create a function, if you want one ready made you can use TF_Find from TF.ahk Textfile & Variables/String Library or http://www.autohotkey.net/~hugov/tf-lib.htm

Code:
;#include tf.ahk ; remove ; if you don't have/want TF in your lib
MsgBox % TF_Find(Text, 1, 0, "What is your name") ; Text is a variable here, if you want to use a file use the filename, "text.txt" for example
see http://www.autohotkey.net/~hugov/tf-lib.htm#TF_Find

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 27th, 2009, 12:45 am 
Offline

Joined: July 28th, 2009, 6:09 pm
Posts: 69
Ok..... I tried again myself and made this, but it doesn't work; why!? it's meant to search every line and if it finds the words 'to be taken' in any line to tell me what line, but it doesn't....

the ahk code:
Code:
#z::
WinGetText, text, test.txt
line=1
retry:
Loop, PARSE, text, `n, `r
If (A_Index = %line%)
{
StringGetPos, match, %A_Index%, to be taken
If ErrorLevel
{
MsgBox, 0, Nope, It is not in line %line%
line+=1
goto, retry
}
Else
{
MsgBox, 0, yes, It is in line %line%
}
}


The Notepad document open at the same time contains (it's called test - Notepad)

Hello
GoodBye
two to be taken once a day
What is your name?
paracetomol
What is your job?
500mg
56 Tablet(s)





thanks


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 27th, 2009, 1:17 am 
Offline

Joined: July 28th, 2009, 6:09 pm
Posts: 69
Don't worry, I was getting mixed up with parsing loops, I hate them :D

Thanks anyway


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 27th, 2009, 4:05 am 
Offline

Joined: March 27th, 2009, 10:48 pm
Posts: 71
Why cant you just read the file?
Tell me exactly what you want it to do.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: December 27th, 2009, 4:07 am 
Offline

Joined: July 28th, 2009, 6:09 pm
Posts: 69
No no honestly it's fine, I've done it now


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Exabot [Bot], WillTroll, XstatyK, Yahoo [Bot] and 19 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group