Jump to content

Recommended Posts

Posted

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)
Posted

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

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

  On 2/21/2013 at 12:50 PM, 'water said:

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.

Posted

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:

  Reveal hidden contents

 

Posted (edited)

  Quote

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:

  Reveal hidden contents
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.

Posted

  On 2/21/2013 at 2:07 PM, 'VelvetElvis said:

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

No problem. :)

  On 2/21/2013 at 2:34 PM, 'PhoenixXL said:

Variables inside quotes and comments would create a problem

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

SciTE Jump can provide a list as well.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

  On 2/21/2013 at 2:08 PM, 'water said:

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.

Posted (edited)

  On 2/21/2013 at 3:23 PM, 'guinness said:

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

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...