Jump to content
Sign in to follow this  
leuce

StringSplit fails every 3 or 4 times

Recommended Posts

leuce

G'day everyone

I have a script that helps me record snippets of text using Windows Sound Recorder. The script reads one line at a time from a file, splits the line into two (the file name and the text to be read), and then helps the user record the text and save the WAV file under the relevant file name.

My problem is that the StringSplit stops splitting correctly after anything from 3 to 6 loops (depending on the length of the string to split).

My code:

#cs

Script to help record a large number of lines.  Script reads file names one by one from a list, and prompts user to speak his line (while an OK msgbox waits), whereupon the user presses enter or space to dismiss the msgbox, and the sound file is saved.

The format of the list file is file-name, tab, text-to-read

#ce

Global $i
Global $part
Global $line
Global $fileopen
Global $fileread

AutoItSetOption ( "WinTitleMatchMode", 2)

$fileopen = FileOpen ("list.txt", "")
$fileread = FileRead ($fileopen, 128)
$line = StringSplit ($fileread, @CRLF, 1)

; Run ("sndrec32.exe") ; optionally, run it yourself before running the script

For $i = 1 to $line[0]

Sleep ("200")

$part = StringSplit ($line[$i], @TAB, 1)
; $part[1] = filename
; $part[2] = text to read

If $part[0] = 2 Then

WinActivate ("Sound Recorder", "")
WinWaitActive ("Sound Recorder", "")

MsgBox (32, "Ready to record?", "FILE: " & $part[1] & ".wav" & @CRLF & @CRLF & "TEXT: " & $part[2], 0)

WinActivate ("Sound Recorder", "")
WinWaitActive ("Sound Recorder", "")

Sleep ("200")

Send ("{SPACE}") ; to start the recording

MsgBox (48, "Recording now...", "[Press OK to stop recording]" & @CRLF & @CRLF & $part[2], 0)

WinActivate ("Sound Recorder", "")
WinWaitActive ("Sound Recorder", "")

Sleep ("200")

Send ("{SPACE}") ; to stop the recording

WinActivate ("Sound Recorder", "")
WinWaitActive ("Sound Recorder", "")

Send ("!f")
Sleep ("200")
Send ("s")
Sleep ("200")
ClipPut (@ScriptDir & "\" & $part[1] & ".wav")
Sleep ("200")
Send ("^a")
Sleep ("200")
Send ("^v")
Sleep ("400")
Send ("{ENTER}")
Sleep ("200")

WinActivate ("Sound Recorder", "")
WinWaitActive ("Sound Recorder", "")

Send ("!f")
Sleep ("200")
Send ("n")
Sleep ("200")

Else
Exit
EndIf

Next

I've built a check into the script to keep it from giving an error message, as you'll see near the top where I check whether $part[0] = 2.

The script would do three or four lines, and then either fail to do the StringSplit or it would split the string in the wrong place. For example, if the list.txt file contains this:

one.wav[tab]This is the house that Jack built

two.wav[tab]This is the car that John drove

three.wav[tab]This is the mouse that the cat ate

...then the script may either stop after having done only two lines, or it may split a line somewhere in the middle of the text after the tab.

I have *no* idea what causes this, but it is getting annoying now that I'm recording longer lines. When I recorded short lines, the script would fail after 5 or 6 lines, but now the script fails after every second line and I have to trim the list.txt file and restart the script each time.

Any ideas?

Thanks

Samuel

Edited by leuce

Share this post


Link to post
Share on other sites
jchd

Can you post a significant sample of input file where you observe unexpected behavior?

Attach it so hat it doesn't get mangled by the posting software.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
GEOSoft

If every line contains a tab character then you could try something like this

#include<array.au3> ;; For test purposes only (_ArrayDisplay)
$sStr = FileRead("somefile.txt")
$aLines = StringRegExp($sStr, "(.+)\t+(.+)(?:\v+|$)", 3)
Dim $aRtn[Ubound($aLines)/2)[2]
For $i = 0 To Ubound($aLines) -2 Step 2
    $aRtn[$i][0] = $aLines[$i]
    $aRtn[$i][1] = $aLines[$i+1]
Next

_ArrayDisplay($aRtn, "New Array")

Warning, written on the fly using your examples so it may need some adjustment.


George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites
leuce

Can you post a significant sample of input file where you observe unexpected behavior?

Well, it simply did not occur to me that the problem might lie with the input file :-). I thought that there must be something obvious in the script that I'm missing. I even recreated the input file, making sure that I don't accidently add anything strange to it.

But later I added MsgBox'es telling me how many items the arrays contain, and I discovered that for some reason the script believes that a 600 line file contains only 7 lines (if the file is split by @CRLF). No amount of fiddling with the input txt got the script to see all 600 lines.

Eventually I changed the way I count lines by using _FileCountLines instead of splitting by line break, and now the script works as expected.

Unfortunately the input file's content is confidential, so I can't share it with you.

But yes, it should have occurred to me that the problem lies not with the script but with the input file.

Thanks

Samuel

Share this post


Link to post
Share on other sites
jchd

In this case and if you didn't find how and why by yourself, masquerade the data so it becomes neutral enough while still having the same formal contents.

Anyway, glad you find out the isue.

Good luck.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites
jvanegmond

Try removing all CR characters with StringStripCR and then StringSplit using only LF as the delimiter.

Share this post


Link to post
Share on other sites
GEOSoft

Try removing all CR characters with StringStripCR and then StringSplit using only LF as the delimiter.

Or conversly, _FileReadToArray() handles it all for them. We can StringSplit on 2 characters (@CRLF = Chr(13) & Chr(10)) but it's always presented problems.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
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
Sign in to follow this  

×