Helix Core is now P4. Product user interfaces will be updated soon. Learn more >>

C/C++ API Documentation | 2025.1

# StrDict::GetVar( const StrPtr & )

Return the value of the specified variable, or `NULL` if not defined.

Virtual? | No |
Class | `StrDict` |
Arguments | `const StrPtr &var` | the name of the variable to look up
Returns | `StrPtr *` | the value, or `NULL` if not defined

## Notes

For the most part, all of the following methods are equivalent:

- `StrDict::GetVar( const StrPtr & )`
- `StrDict::GetVar( const char * )`
- `StrDict::GetVar( const char *, Error * )`
- `StrDict::GetVar( const StrPtr &, int )`
- `StrDict::GetVar( const StrPtr &, int, int )`
- `StrDict::GetVar( int, StrPtr &, StrPtr & )`

The `var` argument must specify the name of a variable in the `StrDict` that you’re trying to look up. In some instances, variables in a `StrDict` are named according to the convention `FOO` `x` or `FOO` `x,y` - one example is the tagged output of `p4 filelog`. Calling `GetVar()` with these numbers as arguments saves you the work of manually constructing the variable name by using `itoa()` and `Append()`.

The version of `GetVar()` that returns an `int` is useful for iterating through a `StrDict`; the `int` argument is an index into the `StrDict`, and the two `StrPtr` arguments are set to contain the variable and value found at that index, if any. This method returns zero if there was no variable at the specified index.

## Example

The implementation of `ClientUser::OutputStat()` in `clientuser.cc` provides a good source example:

Copy

    void ClientUser::OutputStat( StrDict *varList )
    {
        int i;
        StrBuf msg;
        StrRef var, val;

        // Dump out the variables, using the GetVar( x ) interface.
        // Don't display the function, which is only relevant to rpc.
        for ( i = 0; varList-&gt;GetVar( i, var, val ); i++ )
        {
            if ( var == "func" ) continue;

            // otherAction and otherOpen go at level 2, as per 99.1 + earlier
            msg.Clear();
            msg &lt;&lt; var &lt;&lt; " " &lt;&lt; val;
            char level = strncmp( var.Text(), "other", 5 ) ? '1' : '2';
            OutputInfo( level, msg.Text() );
        }

        // blank line
        OutputInfo( '0', "" );
    }

An example of output:

    % p4 -Ztag filelog file.c

    ... depotFile //depot/depot/source/file.c
    ... rev0 3
    ... change0 1949
    ... action0 integrate
    ... type0 text
    ... time0 1017363022
    ... user0 testuser
    ... client0 testuser-luey
    ... desc0 <enter description here>
    ... how0,0 ignored
    ... file0,0 //depot/depot/source/old.c
    ... srev0,0 #1
    ... erev0,0 #2
    ... how0,1 ignored

    ...

Your search for returned results