Sign in to follow this  
Followers 0
VelvetElvis

Find all words starting with...

12 posts in this topic

I'm trying to create a list of all occurrences of variables in an Autoit file. Unfortunately, my regex skills suck badly.

I found a snippet elsewhere here, which at first glance, works pretty well...

$aArray = StringRegExp($varFileContents, "(\$\w{1,50})(?:\s|\[)", 3)

...but doesn't grab variables in situations like this:

_ArraySort($aBefore)

or

_ArrayDelete($aSkills, 0)

Appreciate any help. Here's the code so far:

#include <Array.au3>

; Select file
While 1
$filename = FileOpenDialog("Select a file", @DesktopCommonDir, "AU3 files (*.au3)", 1)
If $filename <> "" Then
ExitLoop
Else
$answer = MsgBox(36, "Check Variables", "No file selected. Exit Program?")
If $answer = 6 Then Exit
EndIf
WEnd

$varFileContents = FileRead($filename)
$aArray = StringRegExp($varFileContents, "(\$\w{1,50})(?:\s|\[)", 3)
_ArraySort($aArray)
_ArrayDisplay($aArray)

Share this post


Link to post
Share on other sites



Have you tried Tidy? It can generate a cross reference listing of variables and functions.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Have you tried Tidy? It can generate a cross reference listing of variables and functions.

Yes, in fact I have, but the results don't seem to be correct: I put this at the top of a file:
#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Run_Tidy=y
#Tidy_Parameters=/gd /gds
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****

And here's the results:

#### indicates that this specific variable only occurs one time in the script.
---- indicates that this specific variable isn't declared with Dim/Local/Global/Const.
== Variables ======================================================================================================
Variable name            Dim Used in Row(s)
========================= ===== ===================================================================================
$UsedOnce                ----- 00011
$aBefore                 ----- 00019 00020
$aSkills                 ----- 00023
$filename                ----- 00012 00017
$myfile              ----- 00022
$test1               ----- 00010
$test2               ----- 00013
$whiz                    ----- 00017 00019

There are several variables that only appear once, as shown above, but they're not indicated with "####", as I would expect. As well, in my test I have this line: "Global $test1[1]", and the Tidy report shows it as not being declared with "Dim/Local/Global/Const".

Perhaps I'm misunderstanding the output.

Share this post


Link to post
Share on other sites

This seems to work.

StringRegExp($sString, "\$\w+", 3)

Yes, a quick test seems to indicate that's what I need. Thanks!!

Share this post


Link to post
Share on other sites

Which version of Tidy do you run? This information is displayed in the Scite output pane.

Tidy AutoIt3 v2.2.2.0 Copyright © Jos van der Zande February 19, 2012


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Yes, a quick test seems to indicate that's what I need. Thanks!!

Variables inside quotes and comments would create a problem

Check this out

#include <Array.au3>


$FileContents = FileRead(@ScriptFullPath)
_ListVars( $FileContents )


Func _ListVars($sString)
$sString = Strip_Comments_Quotes($FileContents)

$aArray = StringRegExp($sString, "(\$\w+)", 3)

;Comment the following two lines for receiving the number of times the vars occured
$aArray = _ArrayUnique( $aArray )
_ArrayDelete( $aArray, 0 )

_ArraySort($aArray)
_ArrayDisplay($aArray)
EndFunc ;==>_ListVars

Func Strip_Comments_Quotes($sString)

Local $Ret, $Comment = False
Local $aArray = StringRegExp($sString, '(?m)(?:[^\r\n]+[\r\n]|[^\r\n]+$)', 3)
;_ArrayDisplay( $aArray )
For $a = 0 To UBound($aArray) - 1
$sString = $aArray[$a]

;Replace the Quotes
$aRet = StringRegExpReplace($sString, '(?s)([''"])(.*?\1)', '')

;Number of Semi-Colon in comments
StringRegExpReplace($aRet, ';', '')
$Comment = @extended

;Replace the Comments
$aRet = StringRegExpReplace($aRet, '(?m)(;.*?$)', '')

;Check Comments
If $Comment Then
;Replace the first semicolon which starts the comment
$sString = StringRegExpReplace($sString, '(.*)(;[^;\r\n]*){' & $Comment & '}', '\1')
EndIf
$Ret &= $sString
Next

Return $Ret
EndFunc ;==>Strip_Comments_Quotes
Edited by PhoenixXL

My code:

PredictText: Predict Text of an Edit Control Like Scite. Remote Gmail: Execute your Scripts through Gmail. StringRegExp:Share and learn RegExp.

Run As System: A command line wrapper around PSEXEC.exe to execute your apps scripts as System (LSA). Database: An easier approach for _SQ_LITE beginners.

MathsEx: A UDF for Fractions and LCM, GCF/HCF. FloatingText: An UDF for make your text floating. Clipboard Extendor: A clipboard monitoring tool. 

Custom ScrollBar: Scroll Bar made with GDI+, user can use bitmaps instead. RestrictEdit_SRE: Restrict text in an Edit Control through a Regular Expression.

Share this post


Link to post
Share on other sites

Yes, a quick test seems to indicate that's what I need. Thanks!!

No problem. :)

Variables inside quotes and comments would create a problem

Yeah, you might want to strip out the comments and strings first.

Share this post


Link to post
Share on other sites

SciTE Jump can provide a list as well.


_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

Which version of Tidy do you run? This information is displayed in the Scite output pane.

Tidy AutoIt3 v2.2.2.0 Copyright © Jos van der Zande February 19, 2012

I was one version back with SciTE, but have since updated.

Here's the output from SciTE

>Running Tidy (2.2.2.0) from:C:\Program Files\AutoIt3\SciTE\tidy
Documentation file:D:\AutoIt\AutoIt scripts\TOOLS\CountVariables\CountVariables_tidy.txt
+>12:53:52 Tidy ended.rc:0

The file created by Tidy shows the output I quoted earlier. The line I don't understand is

"#### indicates that this specific variable only occurs one time in the script."

In the output, there are variables that do only exist once, but they aren't indicated by "####"

Perhaps I don't understand how to tell via the xxxx_tidy.txt which variables occur only once, other than by counting the rows that the vars appear in.

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

SciTE Jump can provide a list as well.

Thanks. I have it in my Tools dropdown, but when I call it up, the list says "No jump data found" is all I see. It's either not installed properly, or more likely, I don't know how to use it.

It definitely helps to run it with an app that has functions. :-)

I don't see how to generate a variables list though.

Edited by VelvetElvis

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