
! script name: vdatatable.d/vextcolsearch
! var_topic: make variable from external table string search

!A
!A NAME:
!A
!A 	Tables: External Column string search variable 	VARIATIONAL
!A
!A DESCRIPTION:
!A
!A	This script creates a variable which is the row name
!A	returned by searching a column of a table for a string
!A	constant.
!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	STRING TO SEARCH FOR:	Input the string to search for.
!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.
!C			 Synergy creates external tables by exporting a
!C			 table as an ascii file.
!C			 External tables may also come from spread sheet
!C			 type programs and from commerical database programs.
!C			 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
!C			as an entity. This may be useful for large tables.
!C			To use an external table as a database each column
!C			must have a unique name placed in row one. Each
!C			row must have a unique name placed in column one.
!C			The unique names must not contain commas or
!C			line feeds or tabs or carriage returns.
!C			If these rules are followed an entry in an external
!C			table may be addressed by its row and column
!C			names. This is important since the row and column
!C			numbers may change if the external table file changes.
!C
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: ext column string search ")

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


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

Tab$= CHR$(9)

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

Search_string:!
Var_search_str=0
stat= SetInputFormat("a99","")
Stat=GetValue("STRING TO SEARCH FOR  Variable ",Answer$)
IF (ExitScript(Destination$)) THEN Leave
SELECT Answer$
CASE "Variable","variable","V","v"
        LowerFileBox
        Stat=UserSelectBox("VARIABLES","selected variable",WsVariationVars,WsVarSelect)
        IF Stat=1 THEN Leave
        Stat=SetInputFormat("a99","")
        Stat=GetValue("NAME OF VARIABLE",Answer$)
        IF (ExitScript(Destination$)) THEN Leave
        IF (Answer$="CANCEL") THEN Start

        N=POS(Answer$,"=")
        IF (N>1) THEN
                 Answer$=Answer$[1,N-1]
                 Answer$=TRIM$(Answer$)
        END IF
        Var_name$=Answer$
        V1=FindNamedVar(Var_name$,WsVariationVars)
        IF NOT ValidGeometry(V1) THEN Start
        Var_search_str=1
        LowerSelectBox

CASE ELSE
Search_str$= Answer$
IF NOT LEN(Search_str$) THEN Search_string
END SELECT

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

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


RecordFunctionOn
Stat= RecordDescr("VARIABLE: Ext column string search")
IF Var_search_str THEN
        V1=FindNamedVar(Var_name$,WsVariationVars)
ELSE
        V1= StringConstant(Search_str$)
END IF
V2= StringConstant(Column_name$)
V3= StringConstant(Filename$)
V4= StringConstant(Tab$)
V5= FreeParameter("Perfect match",1)
V6=CreateVariable(Name$,WsVariationVars,ExtStringSearchCol(V3,V4,V2,V1,V5))
stat= SetVariableDescr(V6,"Ext table row name:")
RecordFunctionOff
CloseVarRecord

GOTO Name_loop


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