User Guide > Creating and Querying Tables > Creating a Table
  

Creating a Table
To use the QUERY_TABLE function, you have to create a table first with the BUILD_TABLE command. Tables are created from vectors (one-dimensional variables) that contain the same number of elements. Each variable becomes, in effect, a column in the table. Before you attempt to create a table, however, you need to read your data into a set of variables.
For detailed information on reading data into PV‑WAVE, see Chapter 9: Working with Data Files in the PV‑WAVE Programmer’s Guide.
For information on creating a table that contains Date/Time data, see "Using Date/Time Data in Tables".
Once your data is read into a set of equal-sized variables, use the BUILD_TABLE function to build a table. Each variable becomes, in effect, a separate column in the table. Once the variables are placed into a table, QUERY_TABLE can be used to subset and manipulate the data.
 
note
In PV‑WAVE, a table is represented as an array of structures. You do not have to understand or use structures to use the table functions. However, you may want to review the chapter on structures, Working with Structures, in the PV‑WAVE Programmer’s Guide, before you proceed to learn about the table functions. Also see the section "Tables and Structures".
The table columns and the original input variables are separate. The original variables are not removed when the table is created.
Example 1: Building a Table
The following example assumes that you have defined eight variables and read data into them. The data for this example represents information collected from a company-wide telephone system. The variable names are: DATE, TIME, DUR, INIT, EXT, COST, AREA, and NUMBER.
The following command builds an eight-column table from the telephone data variables. Note that BUILD_TABLE takes one parameter, a string containing the names of the variables.
phone_data = BUILD_TABLE('DATE, TIME, ' +$
'DUR, INIT, EXT, COST, AREA, NUMBER')
The result is a new table called phone_data, which is shown below.
 
DATE
TIME
DUR
INIT
EXT
COST
AREA
NUMBER
901002
093200
21.40
TAC
311
5.78
215
2155554242
901002
094700
1.05
BWD
358
0.0
303
5553869
901002
094700
17.44
EBH
320
4.71
214
2145559893
901002
094800
16.23
TDW
289
0.0
303
5555836
901002
094800
1.31
RLD
248
.35
617
6175551999
901003
091500
2.53
DLH
332
.68
614
6145555553
901003
091600
2.33
JAT
000
0.0
303
555344
901003
091600
.35
CCW
418
.27
303
5555190
901003
091600
1.53
SRB
379
.41
212
2125556618
901004
094700
.80
JAT
000
0.0
303
555320
901004
094900
1.93
SRB
379
.52
818
8185552880
901004
095000
3.77
DJC
331
1.02
512
5125551228
901004
095100
.16
GWP
370
0.0
303
5551245
 
note
You can format and print a table so that it appears approximately like the above example. For information on printing table data, see "Formatting and Printing Tables".
Using INFO to View the Table Structure
You can use the INFO command to view the table structure. Tables are represented as arrays of structures (for more information on this, see "Tables and Structures"). Thus, the Structure keyword is used with the INFO command to obtain information on tables, for example:
INFO, /Structure, phone_data
** Structure TABLE_0, 8 tags, 40 length:
 
DATE
LONG
901002
TIME
LONG
93200
DUR
FLOAT
21.4000
INIT
STRING
'TAC'
EXT
LONG
311
COST
FLOAT
5.78000
AREA
LONG
215
NUMBER
STRING
'2155554242'
Vectors and BUILD_TABLE
A table is built from vector (one-dimensional array) variables only. You cannot include expressions in the BUILD_TABLE function. For example, the following BUILD_TABLE call is not allowed:
result = BUILD_TABLE('EXT(0:5), COST(0:5)')
However, you can achieve the desired results by performing the array subsetting operations first, then using the resulting variables in BUILD_TABLE. For example:
EXT = EXT(0:5)
COST = COST(0:5)
result = BUILD_TABLE('EXT, COST')
In addition, you cannot include scalars or multidimensional-array variables in BUILD_TABLE.
Example 2: Building a Different Table with the Same Data
From any given set of equal-length variables, BUILD_TABLE can use all or some of the variables to build a table, and the table’s columns can be placed in any order.
The following table contains just four columns instead of eight. Also, the columns appear in a different order than in the previous example.
new_tbl = BUILD_TABLE('DATE,EXT,DUR,COST')
Here is a portion of this new table:
 
DATE
EXT
DUR
COST
901002
311
21.40
5.78
901002
358
1.05
0.0
901002
320
17.44
4.71
901002
289
16.23
0.0
901002
248
1.31
.35
901003
332
2.53
.68
901003
000
2.33
0.0
Example 3: Renaming Columns
By default, BUILD_TABLE uses the original variable names as the names of the table columns. You can rename columns by including the new name or “alias” directly in the BUILD_TABLE command. Place the alias immediately after the original variable name. For example, the previous new_tbl table can be created with different column names:
rename_tbl = BUILD_TABLE('DATE Call_Date, '+$
'EXT Extension, DUR Call_Length,' + 'COST Call_Cost')
The resulting table is identical to the table created in the previous section, except for the column names. To see the structure of this new table, enter:
INFO, /structure, rename_tbl
** Structure TABLE_0, 8 tags, 40 length:
 
CALL_DATE
LONG
901002
EXTENSION
LONG
311
CALL_LENGTH
FLOAT
21.4000
CALL_COST
FLOAT
5.78000

Version 2017.0
Copyright © 2017, Rogue Wave Software, Inc. All Rights Reserved.