KEYS        Creates an array of keys from an existing array

Top  Previous  Next

Syntax:

KEYS

[ existing array ] [*]  [ new array ]

Arguments:

[ existing array ]

An existing array variable.

 

[ new array ]

Array variable to assign; if the variable does not previously exist it is created.

 

This script command creates a new array variable with a zero-based numeric index based on an existing array variable. The elements of the new array are the index values of the existing array. This command is useful when you need to programmatically iterate through elements of an existing array but one or more of the existing array's indices are non-numeric.

 

The following example shows the relationship between the existing array and the new array created by the KEYS command.

 

SET site["address"] = "ftp.acme.com" ;; create array with non-numeric index

DISPLAY site[*]         ;; site[] has one element

DISPLAY site["address"] ;; the value of the first site[] element is "ftp.acme.com"

KEYS site[*] siteIdx     ;; create new siteIdx[] array based on existing array

DISPLAY siteIdx[*]       ;; siteIdx[] also has one element

DISPLAY siteIdx[0]       ;; the value of the first siteIdx[] element is "address"

DISPLAY site[siteIdx[0]] ;; this row displays "ftp.acme.com"

 

The following example shows how to use a counter to loop through each element of an index array created by the KEYS command.

 

SET site["address"] = "ftp.acme.com"

SET site["username"] = "MyLogin"

SET site["password"] = "secret"

KEYS site[*] siteIdx ;; siteIdx[] now contains values: address, username, password

SET size = siteIdx[*] ;; size now = 3

IFNUM= size 0 GOTO loop_done   ;; skip to end if array is empty

SETNUM counter = 0000

:top_of_loop

DISPLAY site[siteIdx[counter]]

INC counter

IFNUM< counter size GOTO top_of_loop

FTPLOGON site["address"] /user=site["username"] /pw=site["password"]

:loop_done

STOP

 

Note: The ordinal sequence in which the non-numeric array items were added to the existing array does not necessarily determine the numeric ordering of elements in the array created by the KEYS command.

 

 

See also: Arrays