
! script name: vdatatable.d/tablemean
! var_topic: find the mean value for  entries in a table row or column.

!A
!A NAME:
!A
!A 	Row/Column Mean		VARIATIONAL
!A
!A DESCRIPTION:
!A
!A 	This script finds the mean value for table entries in a row or column.
!A
!B INSTRUCTIONS:
!B
!B 	PICK START ENTRY: Pick the starting
!B 		entry to be included in the calculation.
!B
!B 	PICK END ENTRY: Pick the ending
!B 		entry to be included in the calculation.
!B
!C NOTES:
!C
!C 	The entries must be in the same table.
!C
!# see also - htdocs/help.d/datatables.htm

stat=ChangeLabelStr("ScriptNameStatus","Function: Row/Column mean ")

DIM Answer$[256]
DIM A$[256]
DIM Destination$[256]
DIM Table1$[256]
DIM Table2$[256]
DIM Table3$[256]
DIM Format$[256]

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

Highlight_pen=3

Format$="decimal 4"

ActivateUndo
Start:!

Element_loop:!
Pick_start:stat= SetInputFormat("n4","")
Element1=PickEntity("PICK START ENTRY ")
IF (ExitScript(Destination$)) THEN Leave
IF NOT ValidDependency(Element1) THEN Pick_start
Stat= EntityNameCheck(Element1,Table1$)
T1= LastPickPara

R1=TableParaRow(Element1,T1)
C1=TableParaColumn(Element1,T1)
Stat= PlotDataTable(Element1,R1,C1,Highlight_pen)

Pick_end:stat= SetInputFormat("n4","")
Element2=PickEntity("PICK END ENTRY ")
IF (ExitScript(Destination$)) THEN 
	Stat= PlotDataTable(Element1,R1,C1,0)
	GOTO Leave
END IF
IF NOT ValidDependency(Element2) THEN Pick_end
Stat= EntityNameCheck(Element2,Table2$)
T2= LastPickPara

IF AreEntitiesSame(Element1,Element2)= WsFalse THEN 
	Stat=SetInputFormat("a1",",P,")
	Stat=GetValue("ENTRIES MUST BE IN SAME TABLE, Proceed",Answer$)
	Stat=SetInputFormat("n4","")
	Stat= PlotDataTable(Element1,R1,C1,0)
	IF ExitScript(Destination$) THEN Leave
	GOTO Element_loop
END IF
R2=TableParaRow(Element2,T2)
C2=TableParaColumn(Element2,T2)
Stat= PlotDataTable(Element1,R1,C1,R2,C2,Highlight_pen)

IF (R1<>R2) AND (C1<>C2) THEN
	Stat=SetInputFormat("a1",",P,")
	Stat=GetValue("ENTRIES MUST BE IN SAME ROW/COLUMN, Proceed",Answer$)
	Stat=SetInputFormat("n4","")
	Stat= PlotDataTable(Element1,R1,C1,R2,C2,0)
	IF ExitScript(Destination$) THEN Leave
	GOTO Element_loop
END IF

Pick_dest:stat= SetInputFormat("n4","")
Element3=PickEntity("PICK DESTINATION ENTRY ")
IF (ExitScript(Destination$)) THEN 
	Stat= PlotDataTable(Element1,R1,C1,R2,C2,0)
	GOTO Leave
END IF
IF NOT ValidDependency(Element3) THEN Pick_dest
Stat= EntityNameCheck(Element3,Table3$)
T3= LastPickPara
R3=TableParaRow(Element3,T3)
C3=TableParaColumn(Element3,T3)

Stat= PlotDataTable(Element3,R3,C3,Highlight_pen)

ActivateUndo
Stat= AdvanceUndoNumber("table mean")
Stat= AddItemToUndo(WsUBModifyMode,Element3)

Stat= EraseEntity(Element3)
IF R1= R2 THEN
RecordFunctionOn
Stat= RecordDescr("TABLE: Row mean")
V1= FreeParameter("Row",R1)
V2= FreeParameter("Start Column",C1)
V3= FreeParameter("End Column",C2)
V4= FreeParameter("Destination Row",R3)
V5= FreeParameter("Destination Column",C3)
V6= FreeParameter("Row sum",TableRowMean(Table1$,V1,V2,V3))
V7= StringConstant(Format$)
Stat=InsertIntoTable(Table3$,V6,V7,V4,V5)
RecordFunctionOff
CloseVarRecord
ELSE
RecordFunctionOn
Stat= RecordDescr("TABLE: Column mean")
V1= FreeParameter("Column",C1)
V2= FreeParameter("Start Row",R1)
V3= FreeParameter("End Row",R2)
V4= FreeParameter("Destination Row",R3)
V5= FreeParameter("Destination Column",C3)
V6= FreeParameter("Column sum",TableColumnMean(Table1$,V2,V3,V1))
V7= StringConstant(Format$)
Stat=InsertIntoTable(Table3$,V6,V7,V4,V5)
RecordFunctionOff
Curr_record= CurrentVarRecord
CloseVarRecord
END IF
Stat= PlotDataTable(Element1,R1,C1,R2,C2,0)
Stat= PlotEntity(Element3)

Stat= AddItemToUndo(WsUBCreateMode,Curr_record)
GOTO Element_loop

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