CygnusX1 Posted December 15, 2014 Posted December 15, 2014 Hey, I found a piece of code that does nearly everything I want it to do. However it is using Regular Expression and I'm horrible at them. I have tried and tried and read the Help and did what I could but I can't figure out for the life of me how to change the Regular Expression to do what I want. I'm trying to get list of all the functions in my current running script including the (). So in my example ConsoleWrite would write: TestCaseOne() TestCaseTwo() .. .. $aArray = StringRegExp(FileRead(@ScriptFullPath), '(?ims)^\s*Func\s*([^(]*)', 3) For $i = 0 To UBound($aArray) - 1 ; Loop through the array. ConsoleWrite($aArray[$i] & @lf) Next I could probably spend the rest of the day reading and trying to learn Regular Expression but I thought I'd ask here first. Thanks for your help. Cygnus
iamtheky Posted December 15, 2014 Posted December 15, 2014 without regex $array = filereadtoarray(@ScriptFullPath) for $i = 0 to ubound($array) - 1 $sNoWS = stringstripws($array[$i], 1) If stringleft($sNoWS , 4) = "Func" Then consolewrite($sNoWS & @CRLF) EndIf Next Func TestCaseOne() EndFunc Func TestCaseTwo() EndFunc ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
qwert Posted December 15, 2014 Posted December 15, 2014 I found this utility script to be a great help in understanding Regular Expressions: '?do=embed' frameborder='0' data-embedContent>> Hope it helps.
Moderators SmOke_N Posted December 15, 2014 Moderators Posted December 15, 2014 "(?i)(?:^|v)s*funcs*(w+)s*(" Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 (edited) Ok, I got the non-regex working, probably easier to maintain going forward. So thanks for that code snippet. Funny thing though, calling the function from the if statement won't execute the function. When I do VarType($nNoWS) It returns a String. $array = filereadtoarray(@ScriptFullPath) for $i = 0 to ubound($array) - 1 $sNoWS = stringstripws($array[$i], 1) If stringleft($sNoWS , 4) = "Func" Then $sNoWS = StringTrimLeft($sNoWS, 5) $sNoWS EndIf Next Func TestCaseOne() EndFunc Func TestCaseTwo() EndFunc So the above snippet doesn't work. Edited December 15, 2014 by CygnusX1 Cygnus
iamtheky Posted December 15, 2014 Posted December 15, 2014 (edited) This pops a msgbox that says "TestCaseOne" for every line that has Func (as it is told to, if you want to also execute TestCaseTwo you need a different trigger). So what is not working exactly? $array = filereadtoarray(@ScriptFullPath) for $i = 0 to ubound($array) - 1 $sNoWS = stringstripws($array[$i], 1) If stringleft($sNoWS , 4) = "Func" Then TestCaseOne() EndIf Next Func TestCaseOne() msgbox(0, '' , "TestCaseOne") EndFunc Func TestCaseTwo() msgbox(0, '' , "TestCaseTwo") EndFunc and here it is with another counter as you roll through functions $array = filereadtoarray(@ScriptFullPath) $x = 1 for $i = 0 to ubound($array) - 1 $sNoWS = stringstripws($array[$i], 1) If stringleft($sNoWS , 4) = "Func" AND $x = 1 Then TestCaseOne() $x+=1 EndIf If stringleft($sNoWS , 4) = "Func" and $x = 2 Then TestCaseTwo() EndIf Next Func TestCaseOne() msgbox(0, '' , "TestCaseOne") EndFunc Func TestCaseTwo() msgbox(0, '' , "TestCaseTwo") exit EndFunc Edited December 15, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
Moderators SmOke_N Posted December 15, 2014 Moderators Posted December 15, 2014 (edited) Ok, I got the non-regex working, probably easier to maintain going forward. So thanks for that code snippet. Funny thing though, calling the function from the if statement won't execute the function. When I do VarType($nNoWS) It returns a String. $array = filereadtoarray(@ScriptFullPath) for $i = 0 to ubound($array) - 1 $sNoWS = stringstripws($array[$i], 1) If stringleft($sNoWS , 4) = "Func" Then $sNoWS = StringTrimLeft($sNoWS, 5) $sNoWS EndIf Next Func TestCaseOne() EndFunc Func TestCaseTwo() EndFunc So the above snippet doesn't work. I'm sorry, I'm a bit confused... How is ^^ that ^^ easier to maintain exactly than: #include <Array.au3> Global $gszFRead = FileRead(@ScriptFullPath) Global $gaFuncs = StringRegExp($gszFRead, "(?i)(?:^|\v)\s*func\s*(\w+)\s*\(", 3) _ArrayDisplay($gaFuncs) Mind you, the #include <Array.au3> and _ArrayDisplay are only there to show you the output Edit: And are you trying to fire functions from strings? You'll need to look at the Call(); func in the help file for that. Edited December 15, 2014 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 Copy and paste error in my above code. I hard coded TestCaseOne() I replaced it with the variable now. Cygnus
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 It is easy for me to maintain because I'm not well versed in RegEX. I understand String manipulation. Cygnus
iamtheky Posted December 15, 2014 Posted December 15, 2014 (edited) yes, we all see the issue (and its not my code). please run both of my snippets in post #6. Edited December 15, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 (edited) Exactly I only noticed the String issue after I try to call the function using the String. When it was printing to the Console I thought it would work but alas, it doesn't? How would you make it work? Edited December 15, 2014 by CygnusX1 Cygnus
iamtheky Posted December 15, 2014 Posted December 15, 2014 (edited) consolewrite($sNoWS)? Instead of calling the testcase functions in the loop. Edited December 15, 2014 by boththose ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__)
mikell Posted December 15, 2014 Posted December 15, 2014 (edited) ? #Include <Array.au3> $res = StringRegExp(FileRead(@ScriptFullPath), 'Func\s*(\w+\([^)]*\))', 3) _ArrayDisplay($res) Func TestCaseZero() msgbox(0, "" , "TestCaseZero") EndFunc Func TestCaseOne($param1 = "0") msgbox(0, $param1 , "TestCaseOne") EndFunc Func TestCaseTwo($param1, $param2) msgbox(0, $param2 , "TestCaseTwo") EndFunc Edited December 15, 2014 by mikell
Moderators SmOke_N Posted December 15, 2014 Moderators Posted December 15, 2014 (edited) Wait wait wait wait!!!!!! @CygnusX1 What EXACTLY do you want to happen and where? Edit: @Mikell, if that's it, then UGH! He specifically asked for a list of functions, not functions and their parameters.... Edited December 15, 2014 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
mikell Posted December 15, 2014 Posted December 15, 2014 Oh, yes In the first post he said that he wants to get the () so if there are params inside, they should be grabbed too - I thought so
Moderators SmOke_N Posted December 15, 2014 Moderators Posted December 15, 2014 Yours is better anyway, it also gets the volatile funcs, I forgot about those. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 I have a script with TestCases, TestCaseOne(), TestCaseTwo(), etc... I want to have a function gather all the function names and put them in an array. Then in a For loop I want to call it of the TestCase functions. $array = filereadtoarray(@ScriptFullPath) ;script path and name for $i = 0 to ubound($array) - 1 ;go through the array $sNoWS = stringstripws($array[$i], 1) ;strip whitespaces If stringleft($sNoWS , 4) = "Func" Then $sNoWS = StringTrimLeft($sNoWS, 5) ;return the function name with () $sNoWS ;TestCaseOne(), TestCaseTwo() etc... EndIf Next Func TestCaseOne() EndFunc Func TestCaseTwo() EndFunc Is that clear? Cygnus
Moderators SmOke_N Posted December 15, 2014 Moderators Posted December 15, 2014 As I said earlier: Call($sNoWs) Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.
CygnusX1 Posted December 15, 2014 Author Posted December 15, 2014 I'm not asking for the parameters, none of my TestCases take parameters. Cygnus
mikell Posted December 15, 2014 Posted December 15, 2014 Yes, Call works nice with the regex from the first post... #Include <Array.au3> $res = StringRegExp(FileRead(@ScriptFullPath), '(?ims)^\s*Func\s*([^(]*)', 3) _ArrayDisplay($res) For $i = 0 to UBound($res)-1 Call($res[$i]) Next Func TestCaseOne() msgbox(0, "" , "TestCaseOne") EndFunc Func TestCaseTwo() msgbox(0, "" , "TestCaseTwo") EndFunc
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