Sign in to follow this  
Followers 0
Catfish

Advance through string one line at a time?

6 posts in this topic

#1 ·  Posted (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 by Catfish

Share this post


Link to post
Share on other sites



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.

Share this post


Link to post
Share on other sites

StringSplit() might be able to help too!


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_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 parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

#4 ·  Posted (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 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

Share this post


Link to post
Share on other sites

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?

Share this post


Link to post
Share on other sites

#6 ·  Posted (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 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

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  
Followers 0