Issues with DataValue() and DataType() functions

I created a database with the following procedure in it to grab all the persistent variables and values out of some databases I’m working on.

Curiously, the “DataValue()” function seems to return an empty string for most values.

Note: The variables _TimeLapseSession and _ManipulateData_t_History have their values populated.

Below is the procedure, in full. The line I’m wondering about is:

vValue = str(datavalue(varName))

It doesn’t seem to matter if I encapsulate the datavalue() function in a str() function or not.

Additionally, the DataType() function seems to only return “Text” for text variables, but does not return the type information for other variables.

Note: all of this is really more a curiosity than anything else. Just wondering if anyone else has run into this.

TIA for any thoughts/suggestions. :slight_smile:

– Mark

/*
** Grab Variables
**
** Walk through a list of databases (dbList) and grab all file, global, and permanent variables
**
** This should be called from a database that has these (all text) fields, named:
**
**   vDB    : name of database that the variable was found in
**   vScope : scope of variable (file, global, or permanent)
**   vName  : name of variable
**   vValue : value of variable (converted to text)
**   vType  : type of variable
**
** Note: this could take a long time to complete, if there are a lot of variables in the databases being scanned
*/

local dbList, varScopeList, dbIndex

dbList = "Customers,Inventory,Transactions"
varScopeList = "File,filevariables:Global,fileglobals:Permanent,permanent"

dbIndex = 1
loop
    local db, varScopeIndex
    db = array(dbList, dbIndex, ",")

    varScopeIndex = 1
    loop
        local varScope, varList, varIndex

        // Get the first item of the "varScopeIndex'th" element of the varScopeList
        varScope = array(array(varScopeList, varScopeIndex, ":"),1,",")

        // Use the second item of the "varScopeIndex'th" element of the varScopeList
        varList = arraysort(dbinfo(array(array(varScopeList, varScopeIndex, ":"),2,","),db), ¶)

        varIndex = 1
        loop
            local varName
            varName = array(varList, varIndex, ¶)
            addrecord
            vDB = db
            vName = varName
            vType = datatype(varName)
            vValue = str(datavalue(varName))
            vScope = varScope
            varIndex = varIndex + 1
        while varIndex <= arraysize(varList, ¶)

        varScopeIndex = varScopeIndex + 1
    while varScopeIndex <= arraysize(varScopeList, ":")

    dbIndex = dbIndex + 1
while dbIndex <= arraysize(dbList, ",")

I wonder if you need to make each database the active database before processing the variables? In PanX this would be something like

db = array(dbList, dbIndex, ",")
setactivedatabase db+""    

but I have to confess I’ve forgotten how that’s done in Pan6 - I think it involved secret windows.

1 Like

That was almost it. The problem is that the variables are out of scope. He needs to stay in the database he is in, because that’s where the fields where he will be storing the values are, but he needs to use grabfilevariable( instead of datavalue(.

I had been racking my brain trying to figure it out, but I was overlooking the fact that the variables were in different databases.

2 Likes

You guys are brilliant. Thank you very much!

Ah! D’oh!
It’s always the simple things …

I actually solved it slightly differently, because I want to grab the “type” as well.
(It’s definitely very “flashy” code)

And, I had to add error checking to catch “undefined” variables.

I’m using this to help me clean up code and see what’s really not being used.

Below is the updated code - in the rare off-chance that it’s of use to anyone else. :slight_smile:

Thanks,

– Mark

/*
** Grab Variables
**
** Walk through a list of databases (dbList) and grab all file, global, and permanent variables
**
** This should be called from a database that has these (all text) fields, named:
**
**   vDB    : name of database that the variable was found in
**   vScope : scope of variable (file, global, or permanent)
**   vName  : name of variable
**   vValue : value of variable (converted to text)
**   vType  : type of variable
**
** Note: this could take a long time to complete, if there are a lot of variables in the databases being scanned
** Note2: this is a very "flashy" routine: you might want to avert your eyes.
*/

local dbList, varScopeList, dbIndex, errorLog

dbList = "Customers,Inventory,Transactions"
varScopeList = "File,filevariables:Global,fileglobals:Permanent,permanent"

errorLog = ""

dbIndex = 1
loop
    local db, varScopeIndex
    db = array(dbList, dbIndex, ",")

    varScopeIndex = 1
    loop
        local varScope, varList, varIndex

        // Get the first item of the "varScopeIndex'th" element of the varScopeList
        varScope = array(array(varScopeList, varScopeIndex, ":"),1,",")

        // Use the second item of the "varScopeIndex'th" element of the varScopeList
        varList = arraysort(dbinfo(array(array(varScopeList, varScopeIndex, ":"),2,","),db), ¶)

        varIndex = 1
        loop
            local varName
            varName = array(varList, varIndex, ¶)

            RememberWindow

            local tType, tValue
            openfile db

            tType = datatype(varName)
            if error
                tType = "<< ERROR Type: "+info("error")+" >>"
            endif

            tValue = datavalue(varName)
            if error
                tValue = "<< ERROR Value: "+info("error")+" >>"
            endif

            OriginalWindow
            
            addrecord
            vDB = db
            vScope = varScope
            vName = varName
            vType = tType // datatype(varName)
            vValue = str(tValue) // datavalue(varName)
            varIndex = varIndex + 1
        while varIndex <= arraysize(varList, ¶)

        varScopeIndex = varScopeIndex + 1
    while varScopeIndex <= arraysize(varScopeList, ":")

    dbIndex = dbIndex + 1
while dbIndex <= arraysize(dbList, ",")