
! script name: vdatatable.d/vexttblentry
! var_topic: make variable from table entry

!A
!A NAME:
!A
!A 	Tables: Assign Variable From An External Table Entry	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script will make a variable from a row column 
!A	entry in an external datatable.
!A
!B INSTRUCTIONS:
!B
!B 	VARIABLE NAME: Enter the variable name.
!B
!B 	INVALID VARIABLE NAME, Proceed: The variable name input was invalid,
!B 		enter "P" to enter a different variable name.
!B
!B 	INPUT ROW NAME: Pick the row name of the entry to be used.
!B
!B 	INPUT COLUMN NAME: Pick the column name of the entry to be used.
!B
!C  NOTES:
!C
!C	External tables: An external table is a tab delimited ascii file
!C 	in which rows are delimited by line feeds.  Synergy creates external
!C 	tables by exporting a table as an ascii file.  External tables may
!C 	also come from spread sheet type programs and from commerical
!C 	database programs.  External tables do not contain entry properties
!C 	such as color, font, text size etc.
!C			 
!C	External table as a database: External tables may be referenced
!C	in synergy without actually importing the table as an entity. This
!C  	may be useful for large tables.  To use an external table as a
!C 	database each column must have a unique name placed in row one. Each
!C 	row must have a unique name placed in column one.  The unique names
!C 	must not contain commas or line feeds or tabs or carriage returns.
!C 	If these rules are followed an entry in an external table may be
!C 	addressed by its row and column names. This is important since the
!C 	row and column numbers may change if the external table file changes.
!C
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: external table entry variable")

DIM Answer$[256]
DIM A$[256]
DIM Name$[256]
DIM Descr$[256]
DIM Element$[256]
DIM Filename$[256]
DIM Row_name$[256]
DIM Column_name$[256]
DIM Destination$[256]
DIM Tab$[32]
DIM Dir$[256]
DIM D_label$[256]
DIM Sub_label$[256]
DIM F_label$[256]
DIM Choice$[256]
DIM Mask$[32]
DIM List$[32000]
DIM List2$[32000]
DEG

Tab$= CHR$(9)

Var_object= CheckVarObject
IF (ExitScript(Destination$)) THEN Leave

DeActivateUndo
Ask_file:!
Filename$=""
Stat= NamedVarStr("datatables dir",WsSystemVars,"/usr/weber/database/tables/",Dir$)
D_label$="DIRECTORY"
Sub_label$="SUB DIRECTORIES"
F_label$="FILES"
Choice$="FILE TO OPEN"
Mask$="[A-Za-z0-9]*"

Stat=UserFileBox(Dir$,Mask$,D_label$,Sub_label$,F_label$,Choice$)
Stat=SetInputFormat("a99","")
Stat=GetValue("SELECT EXISTING ASCII FILE",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$= "CANCEL" THEN Leave
LowerFileBox
IF NamedVarVal("save dirs",WsSystemVars,0) THEN V1=CreateVariable("datatables dir",WsSystemvars,PathNameDirectory(Answer$))
Filename$=Answer$

Name_loop:!
stat= SetInputFormat("a99","")
Stat=GetValue("VARIABLE NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
Name$= Answer$
IF NOT ValidVariableName(Name$)	THEN
	stat= SetInputFormat("a99","")
	Stat=GetValue("INVALID VARIABLE NAME, Proceed",Answer$)
	IF (ExitScript(Destination$)) THEN Leave
	GOTO Name_loop
END IF

Row_name_list:!
D_label$="ROW NAMES"
Stat= ExtTableColumnList(Filename$,Tab$,",",1,List$)
Stat= AlphabeticalOrder(List$,",",WsAscending,List2$)
Stat=GeneralListBox(D_label$,Choice$,List2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT ROW NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Row_name$= Answer$

Column_name_list:!
D_label$="COLUMN NAMES"
Stat= ExtTableRowList(Filename$,Tab$,",",1,List$)
Stat= AlphabeticalOrder(List$,",",WsAscending,List2$)
Stat=GeneralListBox(D_label$,Choice$,List2$)

Stat=SetInputFormat("a99","")
Stat=GetValue("INPUT COLUMN NAME",Answer$)
IF (ExitScript(Destination$)) THEN Leave
IF Answer$="CANCEL" THEN Leave
Column_name$= Answer$
LowerSelectBox



Stat= GetExtTableString(Filename$,Tab$,Row_name$,Column_name$,A$)
IF IsStringNumeric(A$) THEN 
	Numeric_entry=1
ELSE
	Numeric_entry=0
END IF

RecordFunctionOn
Stat= RecordDescr("VARIABLE: External Table Entry")
V1= StringConstant(Row_name$)
V2= StringConstant(Column_name$)
V3= StringConstant(Filename$)
V4= StringConstant(Tab$)
IF Numeric_entry THEN
	V5= FreeParameter("Value",GetExtTableValue(V3,V4,V1,V2))
	V6=NumericVariable(Name$,WsVariationVars,V5)
	stat= SetVariableDescr(V6,"External Table Entry:")
ELSE
	V6=CreateVariable(Name$,WsVariationVars,"  ")
	Stat= GetExtTableString(V3,V4,V1,V2,V6)
	stat= SetVariableDescr(V6,"External Table Entry:")
END IF
RecordFunctionOff
CloseVarRecord

GOTO Name_loop


Leave:!
LowerSelectBox
LowerFileBox
stat= SetInputFormat("n4","")
IF (NOT LEN(Destination$)) THEN
	 LOAD "typecontrol"
ELSE
	 LOAD Destination$
END IF
END
