CGrid:GetCol


The GetCol method returns an array containing the values in a column. The array of column data starts at index 1.

Syntax

array = G:GetCol( nCol )

array = G:GetCol( nCol, nRows )

array = G:GetCol( nCol, nRowMin, nRowMax )

where

    nCol is the column index. Data is retreived from all rows in the column.

    nRows is the number of rows to retrieve, starting at row 1. The row range is clipped to the range [1, GetNumRows].

    nRowMin, nRowMax are the indices of the beginning and ending rows to retrieve. The row range is clipped to the range [1, GetNumRows].

    array is a Lua array containing the data. On failure, nil is returned.

Examples

The following script creates a grid and stores numbers in columns 1 through 5 and rows 1 through 3. The script retrieves data from all of column 3:

G = NewGrid(10,4)

-- new grid with 3 cols, 10 rows, 1 Sheet

G:SetRow( {2,3,4,5,6}, 1 )

-- set 5 columns in row 1

G:SetRow( {4,5,6,7,8}, 2 )

-- set 5 columns in row 2

G:SetRow( {5,6,7,8,9}, 3 )

-- set 5 columns in row 3

t = G:GetCol( 3 )

-- fetch data from row 3

for i = 1,#t do

-- do for each row

  Printf("row[%d]=%lg, ", i, t[i] )

-- result: r[1]=4, r[2]=6, r[3]=7

end

 

The following script creates a grid like the one above. The script retrieves data from 2 rows even though the high row limit extends beyond the end of the grid. Notice that the table starts at index 1 whereas the grid data starts at row nRowMin. There is no data beyond row 3, so its value is 0:

G = NewGrid(10,4)

-- new grid with 3 cols, 10 rows, 1 Sheet

G:SetRow( {2,3,4,5,6}, 1 )

-- set 5 columns in row 1

G:SetRow( {4,5,6,7,8}, 2 )

-- set 5 columns in row 2

G:SetRow( {5,6,7,8,9}, 3 )

-- set 5 columns in row 3

nRowMin = 2 ; nRowMax = 400

-- select rows 2 through 400

t = G:GetCol( 3, nRowMin, nRowMax )

-- get column 4, starting at row 2

for i = 1,#t do

-- do for each row

  Printf("r[%d]=%lg, ", i+nMinRow-1, t[i] )

-- result: r[2]=6, r[3]=7, r[4]=0

end

 

Related Topics

SetCol

GetRow

CGrid class