AutoHotkey Community

It is currently May 25th, 2012, 6:32 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: May 24th, 2007, 5:48 pm 
Offline

Joined: October 12th, 2006, 9:57 pm
Posts: 99
Location: Canada
Hello,

I wonder if it's possible to make a simple clipboard word count with AutoHotKey.
This is the farthest I got, just for testing purposes:

Code:
Loop, parse, clipboard, %A_Space%,
{
   MsgBox, %A_Index%
}
return


I guess I would get it if I could retrieve what is the number of the last loop, that is, the last %A_Index%.
Any suggestion?

Eduardo


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 24th, 2007, 6:24 pm 
Offline

Joined: November 1st, 2005, 6:38 pm
Posts: 114
Location: Ottawa
Try this:
Code:
Loop, parse, clipboard, %A_Space%,
{
   ;MsgBox, %A_Index%
Totalwords = %A_Index%
}

MsgBox, %Totalwords%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 24th, 2007, 6:24 pm 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
Code:
StringSplit, count, Clipboard, %A_space%`n, `r
msgbox, %count0%


This also takes into account carriage returns

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 24th, 2007, 7:05 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5346
Location: UK
A parsing loop outperforms StringSplit, so I'd use ggirf14's method. It can also be done with RegEx, so if you're interested:

Code:
RegExReplace(clipboard, "\b.+?\b", "", count)
count := Ceil(count / 2)
MsgBox, There are %count% words in the clipboard.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 25th, 2007, 12:37 am 
Offline

Joined: October 12th, 2006, 9:57 pm
Posts: 99
Location: Canada
Wow! Thank you fellow users!
I found the script from ggirf14 more accurate.
Titan, I didn't know that a regexmatch could be converted into a number, or am I missing something?

Edu


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 25th, 2007, 1:13 am 
Offline
User avatar

Joined: August 30th, 2005, 8:43 pm
Posts: 8647
Location: Salem, MA
Manual - RegExReplace wrote:
OutputVarCount
The unquoted name of a variable in which to store the number of replacements that occurred (0 if none).

_________________
Image
(Common Answers) - New Tutorials Forum - Humongous FAQ


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 25th, 2007, 6:21 am 
Offline

Joined: December 23rd, 2006, 6:02 pm
Posts: 424
Location: Russia
edu wrote:
I found the script from ggirf14 more accurate.

However I doubt it's accurate enough because it depends on the number of spaces between words. It also reports words to be on the clipboard when there are only spaces there. The same applies to engunneer's example.

Titan's code is the best, though it must depend on what is considered to be word and non-word characters, in which, as far as I know, AutoHotkey is currently fully reliable only for English texts. So a word having a national character in the middle will probably be taken for two words.

Perhaps this variation may be more 'international':
Code:
RegExReplace(Clipboard, "\S(?:\s|$)", "", Count)
Msgbox, % Count


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 25th, 2007, 8:29 am 
StringReplace?
Quote:
When the last parameter is UseErrorLevel, ErrorLevel is given the number occurrences replaced (0 if none). Otherwise, ErrorLevel is set to 1 if SearchText is not found within InputVar, or 0 if it is found.


Code:
StringReplace, OutputVar, ClipBoard, Autohotkey, , UseErrorLevel
MsgBox, The word 'Autohotkey' occurs %ErrorLevel% times within the text.


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

All times are UTC [ DST ]


Who is online

Users browsing this forum: 0x150||ISO, Cerberus, Exabot [Bot], Google [Bot], KenC, Yahoo [Bot] and 66 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