barnabyjonez Posted July 12, 2012 Posted July 12, 2012 Hi everyone, I am having some trouble with a script I wrote that is interacting with a Free Optical Character Recognition (OCR) program. Here is what the script does:1) Open OCR and loads an image2) OCR the image and copies the result to the clipboard. Here is an example of the string this process produces:"1089.1 1076.9 1073.5 1064.5 1057.5 1046.6 1041.4 1040.1 1033.2 1029.8 1029.8 "3) Create an array out of these numbers using StringSplit4) Average the array, send it to another array5) Write the array of averages to a fileThe problem I am having is that StringSplit does not always work as intended. I put in a couple MsgBoxes for the purpose of debugging and it seems like about a quarter of the time it only has 1 item in the array (which is obviously not right). I then run the code again, without making any changes, and it works fine. It defies logic!I found a thread on this forum that seems to address the same problem I am encountering but I am not sure how they ended up solving it. Thread is below:Here is the code. This is literally the first script i have written with autoit, so i'm a pretty big noob. Any advice/help would be greatly appreciated!expandcollapse popup#include<File.au3> ;Create DPSArray Global $DPSArray[8] ;Launch OCR software Run ( "C:\Program Files (x86)\FreeOCR\FreeOCR.exe" ) WinWaitActive("FreeOCR V3 Free OCR Software") ;Open File For $j = 1 To 7 Step 1 Send("!f") Send("{ENTER}") Send("!o") WinWaitActive("Select Image to OCR") ;Pick file Send($j) Send(".jpg") Send("{ENTER}") ;Clear Text Window Send("!t") Send("{ENTER}") ;OCR Send("!o") Send("{ENTER}") ;Remove Line Breaks WinWaitActive("FreeOCR V3 Free OCR Software") Send("!tr") WinWaitActive("FreeOCR V3 Free OCR Software") ;Send to Clipboard Send("!t") Send("{DOWN}") Send("{DOWN}") Send("{ENTER}") ;Average $OCRString = StringReplace(ClipGet(), ". ", ".") $OCRArray = StringSplit($OCRString, " ", 2) $sum = 0 MsgBox(0, "Array Size", UBound($OCRArray)) Sleep(200) For $i = 1 To 10 Step 1 $sum = $sum + $OCRArray[$i] Next $DPSArray[$j] = ($sum/($i-1)) MsgBox(0, "Array Value", $DPSArray[$j]) Next _FileWriteFromArray("C:\Users\Rob\AHscraper\clipboard.txt", $DPSArray)
Moderators Melba23 Posted July 12, 2012 Moderators Posted July 12, 2012 barnabyjonez,Welcome to the AutoIt forum. If you only get the single return from StringSplit it normally means that the function could find no delimiters - so that would mean that there were no spaces between the values in your string. Are all the values going to be ####.# (4 digits followed by a "." and then a single digit? If so we might look to using another technique to extract them from the string. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
jdelaney Posted July 12, 2012 Posted July 12, 2012 Regular expressions route...looks for any number of digits(at least one), where there may be a decimal with any amount of following digits (including none)[autoit]#include <Array.au3>$sString = "1089.1 1076.9 1073.5 1064.5 1057.5 1046.6 1041.4 1040.1 1033.2 1029.8 1029.8 "$aTest = StringRegExp ($sString, "[d]+[.[d]*]?", 3 )_ArrayDisplay ( $aTest )[autoit] IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
John Posted July 13, 2012 Posted July 13, 2012 To test Melba23's idea I would add ConsoleWrite($OCRString & @CRLF) After it was defined. I also noticed you set the StringSplit flag to disable the array count in the first element ($OCRArray[0]), yet you began you For loop at 1. This means you are skipping the first element of the array by starting the loop at $OCRArray[1] instead of $OCRArray[0]. Starting $i at 1 is how you would do it if you hadn't set the StringSplit flag to not put the array element count in $OCRArray[0].
barnabyjonez Posted July 13, 2012 Author Posted July 13, 2012 Wow, thanks for the quick responses guys, I'll give these solutions a shot. To answer your question m23, all values will be x.x - xxxx.x. Thanks for catching that error John, I had previously been running it with the 0 flag on StringSplit but changed it in hopes of solving my problem. I'll get back to you guys after I implement these changes. Thanks again
Moderators Melba23 Posted July 13, 2012 Moderators Posted July 13, 2012 barnabyjonez, If the format is #.# to ####.# then this ought to work: #include <Array.au3> $sText = "1.1 10.9 107.5 1064.5 7.5 46.6 341.4 1040.1 1.2 19.8 129.8 3484.0" $sStripped = StringStripWS($sText, 8) ConsoleWrite($sStripped & @CRLF) $aRet = StringRegExp($sStripped, "d{1,4}.d", 3) _ArrayDisplay($aRet) Please ask if you have any questions. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now