Jump to content

A function to determine variable type


therks
 Share

Recommended Posts

Found one, but I had to modify your code just a little bit.

MsgBox(0, '', _VarDump(0, '', True))

Func _VarDump(ByRef $vVar, $sIndent = '', $IsSupposedToBeUnknown = False)
    Select
        Case $IsSupposedToBeUnknown
            Return 'Unknown(' & $vVar & ')'
        Case IsDllStruct($vVar)
            Return 'Struct(' & DllStructGetSize($vVar) & ')'
        Case IsArray($vVar)
            Return 'Array' & @CRLF & _VarDumpArray($vVar, $sIndent)
        Case IsBinary($vVar)
            Return 'Binary(' & BinaryLen($vVar) & ')'
        Case IsBool($vVar)
            Return 'Boolean(' & $vVar & ')'
        Case IsFloat($vVar)
            Return 'Float(' & $vVar & ')'
        Case IsHWnd($vVar)
            Return 'HWnd(' & $vVar & ')'
        Case IsInt($vVar)
            Return 'Integer(' & $vVar & ')'
        Case IsKeyword($vVar)
            Return 'Keyword(' & $vVar & ')'
        Case IsObj($vVar)
            Return 'Object(' & ObjName($vVar) & ')'
        Case IsString($vVar)
            Return 'String(' & StringLen($vVar) & ') ' & $vVar
        Case Else
            Return 'Unknown(' & $vVar & ')'
    EndSelect
EndFunc
Link to comment
Share on other sites

Found one, but I had to modify your code just a little bit.

MsgBox(0, '', _VarDump(0, '', True))

Func _VarDump(ByRef $vVar, $sIndent = '', $IsSupposedToBeUnknown = False)
    Select
        Case $IsSupposedToBeUnknown
            Return 'Unknown(' & $vVar & ')'
        Case IsDllStruct($vVar)
            Return 'Struct(' & DllStructGetSize($vVar) & ')'
        Case IsArray($vVar)
            Return 'Array' & @CRLF & _VarDumpArray($vVar, $sIndent)
        Case IsBinary($vVar)
            Return 'Binary(' & BinaryLen($vVar) & ')'
        Case IsBool($vVar)
            Return 'Boolean(' & $vVar & ')'
        Case IsFloat($vVar)
            Return 'Float(' & $vVar & ')'
        Case IsHWnd($vVar)
            Return 'HWnd(' & $vVar & ')'
        Case IsInt($vVar)
            Return 'Integer(' & $vVar & ')'
        Case IsKeyword($vVar)
            Return 'Keyword(' & $vVar & ')'
        Case IsObj($vVar)
            Return 'Object(' & ObjName($vVar) & ')'
        Case IsString($vVar)
            Return 'String(' & StringLen($vVar) & ') ' & $vVar
        Case Else
            Return 'Unknown(' & $vVar & ')'
    EndSelect
EndFunc
Am I missing something or did you just modify his code so that it shows you that a variable is unknown if you put the third argument as true? That seems utterly pointless to me...
My Programs[list][*]Knight Media Player[*]Multiple Desktops[*]Daily Comics[*]Journal[/list]
Link to comment
Share on other sites

It's pretty, but what kind of variable would be unknown?

The one you are calling a UDF with, so you need to check it. You want to make sure you can display it/process it properly. Particularly true for array dumps.

David Nuttall
Nuttall Computer Consulting

An Aquarius born during the Age of Aquarius

AutoIt allows me to re-invent the wheel so much faster.

I'm off to write a wizard, a wonderful wizard of odd...

Link to comment
Share on other sites

this is far mostly the most usable function i will ever use since ConsoleWrite ...

BIG THX...

$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

Whooooaaa. I got moved! Haha I guess the example forum is more appropriate now anyway. I've been playing with this a bit more, adding some comments. I also added a _VarDumpNotByRef(), it's basically a wrapper for _VarDump() but, as the title implies, takes the variable not by reference. Using ByRef you're stuck with dumping variables only. For example, I recently was working with a COM object for Winamp, and had this piece of code:

$oWinamp = ObjGet('', 'ActiveWinamp.Application')
If IsObj($oWinamp) Then
    ConsoleWrite(_VarDump($oWinamp.PlayList.GetSelection) & @CRLF)
EndIf

Which threw me this error:

D:\Rob\Desktop\Miniamp\Copy of Miniamp.au3 (8) : ==> Error parsing function call.: 
ConsoleWrite(_VarDump($oWinamp.PlayList.GetSelection) & @CRLF) 
ConsoleWrite(_VarDump($oWinamp^ ERROR

So I created this function:

Func _VarDumpNotByRef($vVar)
    Return _VarDump($vVar)
EndFunc

Which easily bypasses the error. If I submit this for official inclusion (although I don't know that it would make the cut) should I include this wrapper function as well? Or maybe go vice versa and rename the current _VarDump() to _VarDumpByRef() and have _VarDump() be a non-byref compatible function?

*Edit: On a slightly related side note, that piece of code I used as an example gives me an array of the items selected in Winamp's playlist, and when there was nothing selected the function returned this:

Array(
0
)

Weird huh? I looked into it and when there's nothing selected it does indeed return an array... a completely empty array! I even used UBound to check how many subscripts it had, and it returned 0! I wonder how often that comes up, considering we can't even make a completely empty array ourselves (what would be the point anyway?). Scrap that, I made a mistake. Still trying to figure it out though.

Edited by Saunders
Link to comment
Share on other sites

Okay, figured it out.. kinda.

$oWinamp = ObjGet('', 'ActiveWinamp.Application')
If IsObj($oWinamp) Then
    $aSel = $oWinamp.PlayList.GetSelection
    ConsoleWrite(IsArray($aSel) & @CRLF)
    ConsoleWrite(UBound($aSel, 0) & @CRLF)
    ConsoleWrite(UBound($aSel, 1) & @CRLF)
EndIf

Returned:

1

1

0

To get that normally you'd basically have to Dim $array[0] and we can't even do that, not that we have a reason to.

*Edit: I'm just going to have the dumper function return an error string when something like that happens. So now the dump on that variable looks like this:

Array(
    [0] !! Error reading index
)
Edited by Saunders
Link to comment
Share on other sites

  • 5 years later...

I've posted what I believe is a correct version in the thread mentionned. It needs the beta for Null support BTW.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
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
 Share

  • Recently Browsing   0 members

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