AutoIt Script Tutorial 002: String Manipulation
Ξ June 8th, 2008 | → | ∇ AutoIT |
Hopefully you already know how to read in data from a file, if not, you can check out AutoIt Script Tutorial 001: Reading Files. When you’re reading an input file, most of the time each line of your input will contain more than one item of related data. One of the most common formats that I use is a CSV (comma separated value) file. The main reason I use a CSV file is because you can save a spreadsheet as a CSV file, and it’s pretty easy to get any kind of data into a spreadsheet. So here we go…
- Let’s say you have a spreadsheet of last name, first name, area code, phone number…
-
Doe, John, 123, 4657890
Doe, Jane, 231, 6540692
Davis, Rick, 123, 4567890
-
- Save the spreadsheet as a CSV file, I believe most spreadsheet applications will allow you to do this, if not, just make the file manually with Notepad.exe or something.
- So now we have our CSV file, let’s call it “Rick Davis Tutorial 002.csv”.
- Now we need to read the data, see AutoIt Script Tutorial 001: Reading Files if you need an explanation on this…
$RickDavisTutorialInput = FileOpen(”Rick Davis Tutorial 002.csv”, 0)
If $RickDavisTutorialInput= -1 Then
MsgBox(0,”Failure”,”Failed to open file”)
Exit
EndIf
While 1
$myLine = FileReadLine($RickDavisTutorialInput)
If @error = -1 Then ExitLoop
MsgBox(0, “MyLine = “, $myLine)
WEnd
FileClose($RickDavisTutorialInput
- Now lets modify the MsgBox line to break out each of the value’s with each line. To do this, we’re going to use the StringSplit function, which will return an array of values. At this time, I haven’t written an article on arrays within AutoIt so you’ll just have to hold on a little while if you don’t know about arrays. Here’s how the StringSplit function works…
- $arrayVariable = StringSplit($inputString, delimiters, flag)
- $arrayVariable is whatever you want to call the array that is returned from the StringSplit function
- $inputString is whatever string that you want to split apart
- delimiters is the value that you want to use to split the string apart with
- flag is the method in which you want to split the string apart in
- 0 (default) - The string will be split at each character in your delimiters
- 1 - The string will be split at each occurance of all the characters in your delimiters
- $arrayVariable = StringSplit($inputString, delimiters, flag)
- For our example we’ll use this…
- $myLineArray = StringSplit($myLine, “,”, 1)
- This will create an array called $myLineArray
- The contents of the array for the first line will be as follows…
- $myLineArray[0] = 4 - 4 is the number of elements the StringSplit function returned
- $myLineArray[1] = “Doe” - Doe is the first element returned from the StringSplit function
- $myLineArray[2] = ” John” - John is the second element, please not that each of these elements has a preceding space because of the way we typed it into the CSV file
- $myLineArray[3] = ” 123″ - 123 is the third element
- $myLineArray[4] = ” 4657890″ - 4657890 is the forth element
- The contents of the array for the first line will be as follows…
- This will create an array called $myLineArray
- $myLineArray = StringSplit($myLine, “,”, 1)
- Now that we have broken out each of the values from the line lets create a message box with each of the individual values…
- MsgBox(0, “Line Contents:”, “First Name: ” & $myLineArray[2] & @CRLF & “Last Name: ” & $myLineArray[1] & @CRLF & “Area Code: ” & $myLineArray[3] & @CRLF & “Phone Number: ” & $myLineArray[4])
- @CRLF is a Macro that is built in to AutoIt that will create a new line (Carriage Return Line Feed)
- The & between each item in the message box concatenates the items into one long string
- MsgBox(0, “Line Contents:”, “First Name: ” & $myLineArray[2] & @CRLF & “Last Name: ” & $myLineArray[1] & @CRLF & “Area Code: ” & $myLineArray[3] & @CRLF & “Phone Number: ” & $myLineArray[4])
- It will be hard to notice using a message box, but there is an extra space in element 2, 3, and 4 of the $myLineArray array. To remove this you’ll want to use the StringStripWS function either before or during the message box function:
-
- $variableName = StringStripWS($stringToStrip, flag)
- $variableName is the variable in which the stripped string will be stored
- $stringToStrip is the string which you want to strip white spaces from
- flag is the method to strip white spaces
- 1 - Strip white spaces from the beginning
- 2 - Strip white spaces from the end
- 4 - Strip double (or more) white spaces from the string, even between words
- 8 - Strip all spaces
- These numbers can be added together, a flag of 3 will strip white spaces from beginning and end
- $variableName = StringStripWS($stringToStrip, flag)
- So if you want to strip white spaces from beginning and end for all the elements in the array…
- You can do it before the message box:
$myLineArray[1] = StringStripWS($myLineArray[1], 3)
$myLineArray[2] = StringStripWS($myLineArray[2], 3)
$myLineArray[3] = StringStripWS($myLineArray[3], 3)
$myLineArray[4] = StringStripWS($myLineArray[4], 3)
- Or you can do it when you call the message box function:
- MsgBox(0, “Line Contents:”, “First Name: ” & StringStripWS($myLineArray[2], 3) & @CRLF & “Last Name: ” & StringStripWS($myLineArray[1], 3) & @CRLF & “Area Code: ” & StringStripWS($myLineArray[3], 3) & @CRLF & “Phone Number: ” & StringStripWS($myLineArray[4], 3))
- You can do it before the message box:
-
- So in the end you’re code can look like this…
$RickDavisTutorialInput = FileOpen("Rick Davis Tutorial 002.csv", 0)
If $RickDavisTutorialInput = -1 Then
MsgBox(0,”Failure”,”Failed to open file”)
Exit
EndIf
While 1
$myLine = FileReadLine($RickDavisTutorialInput)
If @error = -1 Then ExitLoop
$myLineArray = StringSplit($myLine, ",", 1)
;MsgBox with the white spaces...
MsgBox(0, "Line Contents:", "First Name: " & $myLineArray[2] & @CRLF & “Last Name: ” & $myLineArray[1] & @CRLF & “Area Code: ” & $myLineArray[3] & @CRLF & “Phone Number: ” & $myLineArray[4])
;
;Msgbox with the white spaces stripped out…
MsgBox(0, “Line Contents:”, “First Name: ” & StringStripWS($myLineArray[2], 3) & @CRLF & “Last Name: ” & StringStripWS($myLineArray[1], 3) & @CRLF & “Area Code: ” & StringStripWS($myLineArray[3], 3) & @CRLF & “Phone Number: ” & StringStripWS($myLineArray[4], 3))
;
;Removing the white spaces from each element in the array…
$myLineArray[1] = StringStripWS($myLineArray[1], 3)
$myLineArray[2] = StringStripWS($myLineArray[2], 3)
$myLineArray[3] = StringStripWS($myLineArray[3], 3)
$myLineArray[4] = StringStripWS($myLineArray[4], 3)
;
;Msgbox with displaying each element after already being stripped out…
MsgBox(0, “Line Contents:”, “First Name: ” & StringStripWS($myLineArray[2], 3) & @CRLF & “Last Name: ” & StringStripWS($myLineArray[1], 3) & @CRLF & “Area Code: ” & StringStripWS($myLineArray[3], 3) & @CRLF & “Phone Number: ” & StringStripWS($myLineArray[4], 3))
WEnd
FileClose($RickDavisTutorialInput)
- I know this code doesn’t display pretty on my site, but if you copy & paste it into Scite, and then run the “Tidy AutoIt Source” tool, it looks really pretty o0o0o0o…
- Don’t forget about the other string management functions that AutoIT has…
- StringLeft
- StringRight
- StringMid
- StringTrimLeft
- StringTrimRight
on June 24th, 2008 at 3:09 pm
Any help with this? I can read a CSV file OK with autoit, but the problem I am having is skipping the first line of the CSV. So, I want the script to LOOP through but skip ROW A? Any ideas?
on June 25th, 2008 at 9:45 am
Hi, great to se another person who is finding out about the joys of AutoIT.
I am going through all this stuff about reading in .CSV files and had a question for you.
In the case that the .CSV file has a , in the actual field ie Name John,William How do you handle this as one field?
on October 17th, 2008 at 3:49 am
Is it possible to convert a numerical String to an Integer?
on October 17th, 2008 at 3:54 am
okay, nvm. I just found “StringIsAllNum”