Catfish Posted March 3, 2011 Share Posted March 3, 2011 (edited) Hi there, I am new to AutoIt scriptings but have used basic language before and used PHP quite a bit. I need to be able to iterate through a string (not a file) one line at a time to analyze the contents of each line with a regexp replace method. How can I do this with AutoIt? I looked in the reference manual at all the string functions but there doesn't seem to be any that are designed specifically for something like this. I will keep looking for an answer. PS: I saw StringToASCIIArray. If there was a function like StringToArray would suffice also. So I could just iterate through the array and modify what's required, then convert the array back to a string. Edited March 3, 2011 by Catfish Link to comment Share on other sites More sharing options...
RichardL Posted March 3, 2011 Share Posted March 3, 2011 Hello, You haven't said what the divider is, between the "lines". Assuming it is something fixed, e.g. cr or lf, you could search for it with StringInStr. It takes an "occurance" parameter so you can select the line you want. Get next occurance and you have index for the start and end of the line. I think you can't do StringRegExpReplace on the part of the string in situ. Have to use StringMid to lift out each line to an array as you suggest, do the replacement in the array. Richard. Link to comment Share on other sites More sharing options...
guinness Posted March 3, 2011 Share Posted March 3, 2011 StringSplit() might be able to help too! UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
kylomas Posted March 3, 2011 Share Posted March 3, 2011 (edited) Catfish, You answered your question in your "P.S.". Thi is one technique to process text files: 1 - read file to string 2 - do any global type string processing with regexp (stuff not line specific) 3 - bust string to array with "stringsplit()" as guinness suggested 4 - iterate through the array to do any line specific processing 5 - recreate the string with either "_arraytostring", by re-iterating the array, or, possible in step #4 while processing the array. The following code fragment is an example of this: ; check for duplicates $mst = StringSplit(FileRead($mstfl_name), @CRLF, 2) showL(' # of MST entries before dup chk = ' & UBound($mst)) showL(' Start MST sort') ;quicksort($mst,0,ubound($mst)-1) _ArraySort($mst) showL(' End MST sort') showL(' Start Dup Chk') $tmpmst = '' For $i = 0 To UBound($mst) - 1 If $i = 0 Then ContinueLoop If $mst[$i] = $mst[$i - 1] Then showL(' Dup entry = ' & $mst[$i]) ContinueLoop Else $tmpmst &= $mst[$i] & @CRLF EndIf Next showL(' End Dup Chk') showL(' MST # of unique entries = ' & UBound($mst)) $mstfl = FileOpen($mstfl_name, 2) If $mstfl = -1 Then MsgBox(0, "Error", "Unable to open output file sd0200\sd0210.") Exit EndIf FileWrite($mstfl, $outfl) FileClose($mstfl) kylomas Edit: code changes Edited March 3, 2011 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
Catfish Posted March 3, 2011 Author Share Posted March 3, 2011 I have used $contentArray = StringSplit($string, @crlf) to split the string on new lines. Will there be any issues using StringRegExpReplace on the strings I get from $contentArray if I iterate through the array? Link to comment Share on other sites More sharing options...
kylomas Posted March 4, 2011 Share Posted March 4, 2011 (edited) Catfish, No...you might have to tweak the code snippet that I showed. I ripped it out of a larger process. kylomas Edit: Addendum Check the doc for stringpslit. @CRLF is more than 1 char. Edited March 4, 2011 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
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