Article
· Sep 7, 2016 3m read

NewBie's Corner Session 25 Globals and Arrays Part 1

NewBie's Corner Session 25 Globals, Variables, and Arrays Part 1

Welcome to NewBie's Corner, a weekly or biweekly post covering basic Caché Material.

Globals, Arrays, and Variables

A thorough understanding of Globals, Arrays, and Variables is foundational for every Caché developer.

A Global may be:

Scalar (a single element), as in a Global Variable

Example of Global Variables
Set ^PERSON = "DAVID – DATA ABOUT DAVID"
Set ^PERSON2 = "MICHAEL – DATA ABOUT MICHAEL"
Aggregate (many elements), as in a Global Array. An Array combines similar elements together.
Example of an Global Array
Set ^PEOPLE("DAVID")="DATA ABOUT DAVID"
Set ^PEOPLE("SUSAN")="DATA ABOUT SUSAN"
Set ^PEOPLE("MICHAEL")="DATA ABOUT MICHAEL"
Set ^PEOPLE("AMY")="DATA ABOUT AMY"

The term "Global" is used to refer to either a Global Variable or a Global Array.

Parts of a Global (Array) Name

The Set command
The Global name, consists of the caret and the name – (PEOPLE)
The subscript – contained in parentheses
The equal sign
The data associated with the Global node

A Global (Array) may consists of a number of common elements or a number of non-common elements, as the term multidimensional (multi=many, dimensions) implies.

Example of a number of common elements

Set ^PEOPLE("DAVID")="DATA ABOUT DAVID"
Set ^PEOPLE("SUSAN")="DATA ABOUT SUSAN"
Set ^PEOPLE("MICHAEL")="DATA ABOUT MICHAEL"
Set ^PEOPLE("AMY")="DATA ABOUT AMY"

Example of a multidimensional array

Set ^PEOPLE("DAVID")="DATA ABOUT DAVID"
Set ^PEOPLE("DAVID","SON"")="DATA ABOUT SOLOMON"
Set ^PEOPLE("SUSAN")="DATA ABOUT SUSAN"
Set ^PEOPLE("SUSAN","FATHER")="DATA ABOUT FRANK"
Set ^PEOPLE("SUSAN","FATHER","ADDRESS")="DATA ABOUT AN ADDRESS"
Set ^PEOPLE("MICHAEL")="DATA ABOUT MICHAEL"

In Caché, the term Global can be thought of as data stored on a computer disk. Global data is permanent or persistent, being stored on disk; it will not go away when the computer is shut down.

Persistent Variables and Arrays

A Global refers to persistent or permanent data, stored on disk, which may be variables or arrays.

Non–persistent Variables and Arrays

Variables and Arrays typically are non–persistent, that is, not stored on disk, but only stored in memory. These Variables and Arrays cease to exist when the computer shuts down or the process that created them ceases.

If it helps, you can think of Variables as "Local Variables", and Arrays as "Local Arrays", as opposed to "Global Variables" and "Global Arrays."

But bear in mind, when a developer refers to a Variable or Array, most likely they are referring to a Local Variable and Local Array. Otherwise, they will be called Global Variables and Global Arrays. Or just Globals, not making a distinction between variables or Arrays.

Global variables and arrays are handled just like local variables and arrays with the exception of the pre–ceding caret (^).

The only difference is that local variables and arrays exist only in memory, Global variables and arrays (their name being preceded by the caret (^) ) are persistent and exist on disk. They are only removed when explicitly killed.

To summarize:

–             Global Arrays are Multidimensional and Persistent
–             Global Variables are One–Dimensional and Persistent
–             Local Arrays are Multidimensional and not Persistent
–             Local Variables are One–Dimensional and not Persistent

Using the vernacular of other computer languages, Globals may be thought of as files. They reside on disk and are permanent, or persistent, until deleted. However, Globals are unique to Cache. They are not read in a sequential manner like traditional files. Be very careful when killing any Global. An entire database may be wiped out in an instant with one simple Kill command. Unfortunately, there is no warning that a Global is being deleted and no undelete capability. The Kill Global command may delete thousands or even millions of descendant nodes. The Kill is a powerful and dangerous command.

--Mike Kadow

PaulMikeKadow@gmail.com

If you have a comment, please respond through the InterSystems Developer Community, don't send me private email, unless of course you wish to address me only.

See "Newbie's Corner Index" for an index of all NewBies' Posts

Discussion (0)0
Log in or sign up to continue