Jump to content

StringSplit not working consistently


Recommended Posts

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 image

2) 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 StringSplit

4) Average the array, send it to another array

5) Write the array of averages to a file

The 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!

#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)
Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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].

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...