Jump to content

Recommended Posts

Posted

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

Posted

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

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

  • Moderators
Posted

"(?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.

Posted (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 by CygnusX1

Cygnus

Posted (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 by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

  • Moderators
Posted (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 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.

Posted (edited)

yes, we all see the issue (and its not my code).  please run both of my snippets in post #6. 

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Posted (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 by CygnusX1

Cygnus

Posted (edited)

consolewrite($sNoWS)?

Instead of calling the testcase functions in the loop.

Edited by boththose

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

  • Moderators
Posted (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 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.

  • Moderators
Posted

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.

Posted

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
Posted

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.

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...